1

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;*> 
1
  • you could add the token for dot matches line breaks. Maybe something like Pattern.DOTALL Commented Feb 1, 2014 at 4:20

2 Answers 2

2

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()); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Updated with multiline and it still will not find the matches
Multiline has to do with the behavior of ^$ I don't think that's what he needs.
@instanceOfObject Awesome, works great. One Question, if I am writing that multiline code to a file, is there any way to write it out all on one line instead of each individual like as it is in the source code?
1

Try adding Pattern.DOTALL.

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

75px(.*)center 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.