0

Possible Duplicate:
Extract text and links from HTML using Regular Expressions

Given an string containing HTML such as:

       <td scope="row">1</td> 
        <td scope="row"></td> 
        <td scope="row"><a href="/Archives/directory.htm">directoryfile.htm</a></td> 
        <td scope="row">directoryfile</td> 
        <td scope="row">104569</td> 
     </tr> 
     <tr class="blueRow"> 
        <td scope="row">2</td> 
        <td scope="row"></td> 
        <td scope="row"><a href="/Archives/historicaldata.htm</a></td> 
        <td scope="row">historicaldata</td> 
        <td scope="row">40361</td> 
     </tr> 
     <tr> 
        <td scope="row">&nbsp;</td> 
        <td scope="row"><span class="blue">Complete submission text file</span></td> 
        <td scope="row"><a href="/Archives/businessagenda.txt</a></td> 
        <td scope="row">businessagenda;</td> 
        <td scope="row">146701</td> 

I want to just grab the link for historicaldata using regex. However, it seems that my program is not finding the link and I do not see the problem as the regex works on the tester. Can you guys see what's the problem?

I understand that regex is not the best thing to use with HTML but I just want to try it. Thanks everyone!

Pattern data = Pattern.compile("Archives.*\s.*historicaldata");
Matcher test1= data.matcher(inputHTML);

while (test1.find()) {
System.out.println("Test: Now matching"); // Doesn't print
}

Community
  • 1
  • 1
Jacob K.
  • 21
  • 1
  • 4
    Duck, before the "ARRGGH! Parsing Html with Regex!" crowd throws a shoe at you. – Robert Harvey Aug 10 '10 at 19:38
  • 1
    Too late, [shoe's already been thrown](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – NullUserException Aug 10 '10 at 19:43
  • 2
    I'll see your "parsing html with regex" and raise you "tables for layout", "css class named for color", and ".htm instead of .html in filename" – Stephen P Aug 10 '10 at 19:46

2 Answers2

0

If you are just wanting to match 'Archives\historicaldata' then your regex string should be "Archives\/historicaldata" in fact you may be able to use "Archives/historicaldata"

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
0

In your pattern "Archives.*\s.*historicaldata"

Pattern data = Pattern.compile("Archives.*\s.*historicaldata");

the \s means whitespace[1], and since there is no whitespace in "/Archives/directory.htm" it doesn't match. Try just

Pattern data = Pattern.compile("Archives.*historicaldata");

[1] The "\s" is incorrect also - to get that in the pattern you have to escape the backslash so it becomes "Archives.*\s.*historicaldata"

Stephen P
  • 14,422
  • 2
  • 43
  • 67