简体   繁体   中英

regex in java extract a number

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM