Regular expressions help!

I'm trying to pull a seven digit number out of a string of text. I only need the number but the text before and after it will change every time. I'm reading the html in line by line but need to only get the second 7 digit number from this string

</td><td align="left" width="15%" class="text"><a CLASS="Bcruising" HREF="&#xA; javascript:EditMember(2566802,2966456)&#xA; ">Usernamexxx</a>

I would use this command in bash "egrep -o '[0-9]{7}' " and am looking for something similar in java.

Any help would be very appreciated :)

Posted Jul 12, 2012, 6:24 PM UTC

42
mbdouche•

Did you try using the Pattern and Matcher classes from Java? Something like:

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


Pattern p = Pattern.compile("([0-9]{7})");
Matcher m = p.matcher(inString);

// find 2nd instance of pattern and print it
if (m.find() && m.find()) {
    System.out.println(m.group(1));
}
3
IrrationalmachineOP•

Thank you. It worked like a charm.

1