Ctech45
Ctech45

Reputation: 496

How to find pattern with Pattern and Matcher classes?

I am writing a program that needs to check where a group of characters exist. My code is currently:

String checkerLoop = "ForeclosureResutls_CaseNum_"; 
          Pattern checkerLoopPattern = Pattern.compile("(?<="+Pattern.quote(checkerLoop)+").*?(?="+checkerNumber+")");
          Matcher checkerLoopMatcher = checkerLoopPattern.matcher(scraper.getPage().getWebResponse().getContentAsString()); 

          while (checkerLoopMatcher.find()) {
            checker = true;
          }

The sentence I need to look for is "ForeclosureResutls_CaseNum_"+ checkerNumber, where checker number is an int. I tried writing this code based off previous code to find a set of characters in between two groups, so I believe that may be why this code is not working properly.

The sample input string would be as follows:

<a id="SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_0" href="javascript:__doPostBack(&#39;ctl00$SheetContentPlaceHolder$ctl00$gvForeclosureResutls$ctl02$lbCaseNum&#39;,&#39;&#39;)" style="display:inline-block;width:100px;">CV-13-798497</a>
                    </td><td align="center">488-05-029</td><td align="center">I</td><td align="center">01/02/2013</td>
        </tr><tr style="background-color:Gainsboro;">
            <td align="left">UNKNOWN HEIRS, ETC OF D.C. RUFUS, ET AL  </td><td align="left">10603 HAMPDEN AVENUE</td><td align="center">CLEVELAND</td><td align="center">44108-0000</td><td align="center">
                        <a id="SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_1" href="javascript:__doPostBack(&#39;ctl00$SheetContentPlaceHolder$ctl00$gvForeclosureResutls$ctl03$lbCaseNum&#39;,&#39;&#39;)" style="display:inline-block;width:100px;">CV-13-798498</a>
                    </td><td align="center">109-16-094</td><td align="center">A</td><td align="center">01/02/2013</td>
        </tr><tr style="background-color:LightGrey;">
            <td align="left">SHARECE MILLER, ET AL  </td><td align="left">13514 ALVIN AVENUE</td><td align="center">GARFIELD HTS</td><td align="center">44105-0000</td><td align="center">
                        <a id="SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_2" href="javascript:__doPostBack(&#39;ctl00$Shee

Upvotes: 2

Views: 82

Answers (1)

aliteralmind
aliteralmind

Reputation: 20163

Okay, so this is what I've got. I'm not fulfilling your requirements exactly, but this should be good to get you on the right path.

First of all ForeclosureResutls_CaseNum_ is not found at all in this demo data. ForeclosureResutls_lbCaseNum is, so that's what I've gone with.

Also, I'm ignoring the checkerNumber and assuming you want to check for any number, as there are three in this input, and I don't know how yours are derived. Hence the \\d.

The regex you were using in your post is crazy, given what you need to do, as far as I understand it. The one I've used is trivial in comparison.

Try this:

import  java.util.regex.Matcher;
import  java.util.regex.Pattern;

/**
   <P>{@code java ParseForclosureResultsXmpl}</P>
 **/
public class ParseForclosureResultsXmpl  {
   public static final void main(String[] igno_red)  {
      String sLS = System.getProperty("line.separator", "\n");

      StringBuilder sdInput = new StringBuilder().
         append("<a id=\"SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_0\" href=\"javascript:__doPostBack(&#39;ctl00$SheetContentPlaceHolder$ctl00$gvForeclosureResutls$ctl02$lbCaseNum&#39;,&#39;&#39;)\" style=\"display:inline-block;width:100px;\">CV-13-798497</a>").append(sLS).
         append("              </td><td align=\"center\">488-05-029</td><td align=\"center\">I</td><td align=\"center\">01/02/2013</td>").append(sLS).
         append("  </tr><tr style=\"background-color:Gainsboro;\">").append(sLS).
         append("      <td align=\"left\">UNKNOWN HEIRS, ETC OF D.C. RUFUS, ET AL  </td><td align=\"left\">10603 HAMPDEN AVENUE</td><td align=\"center\">CLEVELAND</td><td align=\"center\">44108-0000</td><td align=\"center\">").append(sLS).
         append("                  <a id=\"SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_1\" href=\"javascript:__doPostBack(&#39;ctl00$SheetContentPlaceHolder$ctl00$gvForeclosureResutls$ctl03$lbCaseNum&#39;,&#39;&#39;)\" style=\"display:inline-block;width:100px;\">CV-13-798498</a>").append(sLS).
         append("              </td><td align=\"center\">109-16-094</td><td align=\"center\">A</td><td align=\"center\">01/02/2013</td>").append(sLS).
         append("  </tr><tr style=\"background-color:LightGrey;\">").append(sLS).
         append("      <td align=\"left\">SHARECE MILLER, ET AL  </td><td align=\"left\">13514 ALVIN AVENUE</td><td align=\"center\">GARFIELD HTS</td><td align=\"center\">44105-0000</td><td align=\"center\">").append(sLS).
         append("                  <a id=\"SheetContentPlaceHolder_ctl00_gvForeclosureResutls_lbCaseNum_2\" href=\"javascript:__doPostBack(&#39;ctl00$Shee").append(sLS);

      String sRqdValuePrefix = "ForeclosureResutls_lbCaseNum_";
      Pattern checkerLoopPattern = Pattern.compile(sRqdValuePrefix + "\\d");
      Matcher m = checkerLoopPattern.matcher("");  //Unused. so the matcher can be reused in the loop.

      int iLn = 0;
      String[] asInput = sdInput.toString().split(sLS);
      for(String s : asInput)  {
         iLn++;    //1st iteration: Was zero, now 1

         //Resuing matcher instead of retrieving new one from Pattern each iteration
         m.reset(s);

         if(m.find())  {
            int iCheckerNumber = Integer.parseInt(s.substring(m.start() + sRqdValuePrefix.length(), m.end()));
            System.out.println("Found on line " + iLn + ", at index " + m.start() + " with checker number " + iCheckerNumber);
         }
      }
   }
}

Output:

[C:\java_code\]java ParseForclosureResultsXmpl
Found on line 1, at index 39 with checker number 0
Found on line 5, at index 57 with checker number 1
Found on line 9, at index 57 with checker number 2

Ask any questions.

Upvotes: 1

Related Questions