Ctech45
Ctech45

Reputation: 496

Pattern/Matcher not working with line break

I have written a program using Pattern and Matcher in Java to produce a string in between a group of characters. My code is currently:

    String trying = "75px;"; 
    Pattern tryingPattern = Pattern.compile("(?<="+Pattern.quote(trying)+").*?(?=center)", Pattern.MULTILINE);
    Matcher tryingMatcher = tryingPattern.matcher(pageContentString); 

       while (tryingMatcher.find()) {
           docketFile.write(tryingMatcher.group().toString());
    }

I am attempting to obtain the information between the "75px;" and and the word "center". The issue is that any time a sequence comes up as shown below with the information on several lines, it is not recognizing the pattern even though the beginning and ending string exist. I am not sure based off my code why information including line breaks will not allow the pattern to be recognized.

Sample data below.

 <td align=*left* valign=*top* style=*width:75px;*>03/04/2013</td><td align=*left* valign=*top*>D6</td><td align=*left* valign=*top*>SR</td><td align=*left*>SUMS AMENDED COMPLNT(20967973) SENT BY CERTIFIED MAIL.
    TO:
    CUYAHOGA CLERK OF COURTS
    1200 ONTARIO CT
    CLEVELAND, OH 44113-0000
    </td><td align=*center*><a href=*DisplayImageList.aspx?q=03WzlSkU6oMVIiKW14aCZBTEV4FirUMU0*><img src=*images/ImageSheet.png* alt=** /></a></td>
            </tr><tr style=*background-color:Gainsboro;*>

Upvotes: 1

Views: 263

Answers (2)

instanceOfObject
instanceOfObject

Reputation: 2994

Pattern.MULTILINE affects the behavior of ^ and $ (to match the beginning and the end of the line), which you don't use at all in your pattern.

For your use case, use DOTALL as you want all the content to be treated in the single line.

This will work!

String trying = "75px;"; 
Pattern tryingPattern = Pattern.compile("(?<="+Pattern.quote(trying)+").*?(?=center)", Pattern.DOTALL);
Matcher matcher = tryingPattern.matcher(str);
// check all instances
while (matcher.find()) {
  System.out.println(matcher.group());
}

Upvotes: 2

Ron Rosenfeld
Ron Rosenfeld

Reputation: 60474

Try adding Pattern.DOTALL.

Another option to capture that text would be to use a capturing group:

75px(.*)center

Upvotes: 1

Related Questions