1

Given the String:

<td>4</td><td>punz of damage</td><td><img src='images/no.png'></img></td><td>May 26, 2015 10:28:12 PM</td><td>30</td><td>Nov 26, 2017 10:28:12 PM</td>

I would like to be able to return only the value between the second element.

How would I accomplish this? I have the following so far:

    private static Pattern p = Pattern.compile("<td>(.+?)</td>");

public static String getName(String in) {
    Matcher m = p.matcher(in);

    if (m.matches()) {
        return m.group(1);
    } else {
        return null;
    }
}
1
  • 1
    You shouldn't be using regexes on html. use a DOM parser. xpath makes this sort of thing beyond trivial. Commented May 28, 2015 at 18:02

1 Answer 1

1

Use matcher.find() in a loop instead of matches and keep a counter:

private static Pattern p = Pattern.compile("<td>(.+?)</td>");

public static String getName(String in) {
    Matcher m = p.matcher(in);

    for (i=0; i<1 && m.find(); i++);

    if (i==0) {
        return null;
    } else {
        return m.group(1);
    }
}

Caution: Parsing HTML/XML using regex can be error prone.

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

1 Comment

Thank you! I knew there had to be an easy way of accomplishing what I needed. I believe it's time I take another crack and reading over the documentation and understanding it.

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.