[英]Matching non multiline regular expression
I have the following file contents and I'm trying to match a reg explained below: 我具有以下文件内容,并且我试图匹配下面说明的reg:
-- file.txt (doesn't match single/in-line text) --
test On blah more blah wrote:
blah blah blah
blah blah
blah
---------------
If I read the file contents from above to a String and try to match the "On...wrote:" part I cannot get a match: 如果我从上方将文件内容读取为字符串,然后尝试匹配“ On ... wrote:”部分,则无法找到匹配项:
// String text = <file contents from above>
Pattern PATTERN = Pattern.compile("^(On\\s(.+)wrote:)$");
Matcher m = PATTERN.matcher(text);
if (m.find()) {
System.out.println("Never gets HERE???");
// TODO: Strip out all characters after the match and any \s or \n before
}
Essentially i want to the following output: 本质上,我想要以下输出:
-- file2.txt --
test
---------------
Maybe this helps you get the result you want: 也许这可以帮助您获得所需的结果:
String text = "test On blah more blah wrote:\n"
+ "blah blah blah\nblah blah\nblah\n";
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Pattern PATTERN = Pattern.compile("^(.*?)\\s*On\\s(.+)wrote:$",
Pattern.MULTILINE);
Matcher m = PATTERN.matcher(text);
if (m.find()) {
pw.println(m.group(1));
}
pw.close();
System.out.println(sw);
Pattern.MULTILINE javadoc: In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator ... I also added the (.*?) which matches everything before the first "On". Pattern.MULTILINE javadoc: 在多行模式下,表达式^和$分别在行终止符之后或之前匹配 。我还添加了(。*?),它匹配第一个“开”之前的所有内容。
since the pattern you are looking for doesn't start the line, remove the ^
. 由于您要查找的模式无法开始,因此请删除
^
。 This matches the beginning of a line, but your the line you are looking for starts with the word "test". 这与行的开头匹配,但是您要查找的行以单词“ test”开头。
However if you want to capture the "test", then insert (\\\\w+)\\\\s
after the ^
to form ^(\\\\w+)\\\\s(On\\\\s(.+)wrote:)$
但是,如果要捕获“测试”,则在
^
后面插入(\\\\w+)\\\\s
,以形成^(\\\\w+)\\\\s(On\\\\s(.+)wrote:)$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.