Reputation: 13357
I have the following file contents and I'm trying to match a reg explained below:
-- 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:
// 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
---------------
Upvotes: 0
Views: 64
Reputation: 3464
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".
Upvotes: 1
Reputation: 3650
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".
However if you want to capture the "test", then insert (\\w+)\\s
after the ^
to form ^(\\w+)\\s(On\\s(.+)wrote:)$
Upvotes: 0