1

i am getting started with regex in java and am trying to extract the number 99999 from a String which looks like:

<result name="response" numFound="99999" start="0">

Can you suggest me what can be the most efficient regex to achieve that? Thanks!

0

2 Answers 2

2

If this is a one-off case, you can use the Pattern and Matcher classes from java.util.regex package as follows and extract the value:

Pattern pattern = Pattern.compile("numFound=\"([0-9]+)\"");
Matcher matcher = pattern.matcher("<result name=\"response\" numFound=\"99999\" start=\"0\">");

if (matcher.find())
{
    System.out.println(matcher.group(1));
}

Otherwise, it is strongly recommended to use a proper HTML Parser like Jericho to parse the HTML and read the attributes accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Vikdor. I actually tried something like this and that works. Thanks again.
1

Use replaceAll() to extract the part you want in just one line.

String number = input.replaceAll(".*numFound=\"(\\d+).*", "$1");

Here's some test code:

public static void main(String[] args) {
    String input = "<result name=\"response\" numFound=\"99999\" start=\"0\">";
    String number = input.replaceAll(".*numFound=\"(\\d+).*", "$1");
    System.out.println(number);
}

Output:

99999

Comments

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.