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

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -