java - RegEx Exepression not matching -
i have following text
chapter 1 introduction chapter overview
which did create , tested (http://regexr.com/) following regex for
(chapter\s{1}\d\n)
however when use following code on java fails
string text = stripper.gettext(document);//the text above pattern p = pattern.compile("(chapter\\s{1}\\d\\n)"); matcher m = p.matcher(text); if (m.find()) { //do action }
the m.find() returns false.
your document may have dos line feed \r
well. can use either of these patterns:
pattern p = pattern.compile("chapter\\s+\\d+\\r");
\r
(requires java 8) match combination of \r
, \n
after digits or use:
pattern p = pattern.compile("chapter\\s+\\d+\\s");
since \s
matches whitespace including newline characters.
another alternative use multiline
flag anchor $
:
pattern p = pattern.compile("(?m)chapter\\s+\\d+$");
Comments
Post a Comment