0

I have

String content= "<a data-hovercard=\"/ajax/hovercard/group.php?id=180552688740185\">
                 <a data-hovercard=\"/ajax/hovercard/group.php?id=21392174\">"

I want to get all the id between "group.php?id=" and "\""

Ex:180552688740185

Here is my code:

String content1 = "";
Pattern script1 = Pattern.compile("group.php?id=.*?\"");
Matcher mscript1 = script1.matcher(content);
while (mscript1.find()) {
    content1 += mscript1.group() + "\n";
}

But for some reason it does not work.

Can you give me some advice?

1
  • Try using String.match or String.split -- both take regex. this might help forums.codeguru.com/… Commented Oct 15, 2012 at 14:56

1 Answer 1

2

Why are you using .*? to match the id. .*? will match every character. You just need to check for digits. So, just use \\d.

Also, you need to capture the id and then print it.

// To consider special characters as literals
String str = Pattern.quote("group.php?id=") + "(\\d*)";

Pattern script1 = Pattern.compile(str);
// Your matcher line
while (mscript1.find()) {
    content += mscript1.group(1) + "\n";   // Capture group 1 contains your id
}
Sign up to request clarification or add additional context in comments.

7 Comments

you also need to escape the ? Pattern.compile("group.php\\?id=(.*?)\"");
I try that way too but the return string is still null.
@HirijiSakumo. Great. Just missed that `\` thanks to Shyam that it got corrected. :)
regexp can be reduced to: "group.php\\?id=(\\d*)" to extract the whole number.
@Mik378 Oh yes.. Non-greedy quantifier is not needed here. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.