使用正则表达式匹配多行文本 [英] Match multiline text using regular expression
问题描述
我正在尝试使用java匹配多行文本。当我使用 Pattern
类与 Pattern.MULTILINE
修饰符时,我能够匹配,但我无法使用(?m)进行此操作。
I am trying to match a multi line text using java. When I use the Pattern
class with the Pattern.MULTILINE
modifier, I am able to match, but I am not able to do so with (?m).
与相同的模式(?m )
并使用 String.matches
似乎不起作用。
The same pattern with (?m)
and using String.matches
does not seem to work.
我肯定我错过了什么,但不知道是什么。我不太擅长正则表达式。
I am sure I am missing something, but no idea what. Am not very good at regular expressions.
这就是我试过的事情
String test = "User Comments: This is \t a\ta \n test \n\n message \n";
String pattern1 = "User Comments: (\\W)*(\\S)*";
Pattern p = Pattern.compile(pattern1, Pattern.MULTILINE);
System.out.println(p.matcher(test).find()); //true
String pattern2 = "(?m)User Comments: (\\W)*(\\S)*";
System.out.println(test.matches(pattern2)); //false - why?
推荐答案
首先,你在不正确的情况下使用修饰符假设。
First, you're using the modifiers under an incorrect assumption.
Pattern.MULTILINE
或(?m)
告诉Java接受锚点 ^
和 $
以匹配每行的开头和结尾(否则它们只是匹配整个字符串的开头/结尾。)
Pattern.MULTILINE
or (?m)
tells Java to accept the anchors ^
and $
to match at the start and end of each line (otherwise they only match at the start/end of the entire string).
Pattern.DOTALL
或 (?s)
告诉Java允许点匹配换行符。
Pattern.DOTALL
or (?s)
tells Java to allow the dot to match newline characters, too.
其次,在你的情况下,正则表达式失败,因为你使用 matches()
方法,该方法要求正则表达式匹配整个字符串 - 由于存在一些字符,这当然不起作用在(\\ W)*(\\ S)*
匹配后离开。
Second, in your case, the regex fails because you're using the matches()
method which expects the regex to match the entire string - which of course doesn't work since there are some characters left after (\\W)*(\\S)*
have matched.
所以如果你只是想找一个以用户评论:
开头的字符串,请使用正则表达式
So if you're simply looking for a string that starts with User Comments:
, use the regex
^\s*User Comments:\s*(.*)
Pattern.DOTALL
选项:
Pattern regex = Pattern.compile("^\\s*User Comments:\\s+(.*)", Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group(1);
}
ResultString
然后包含用户评论后的文字:
这篇关于使用正则表达式匹配多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!