使用正则表达式匹配多行文本 [英] Match multiline text using regular expression
问题描述
我正在尝试使用 java 匹配多行文本.当我使用带有 Pattern.MULTILINE
修饰符的 Pattern
类时,我能够匹配,但我不能使用 (?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 a a
test
message
";
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.
因此,如果您只是在寻找以 User Comments:
开头的字符串,请使用正则表达式
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
然后将包含 User Comments:
这篇关于使用正则表达式匹配多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!