1

I have the following html line

<b>String :</b></b></td><td class="title">14</td>

I'm trying to parse it on order to get number only. Looks simple but "s/^.*\(:digit:\).*$/\1/" shows whole line. I tried also "s/^.*\(\d+\).*$/\1/" but it return the same result.

If try "s/^.*String.*>\(.*\)<.*$/\1/" command then it returns what is needed but "s/^.*String.*>\(\d+\)<.*$/\1/" returns again whole line.

Do you think is possible to get here number from the string specifying include only digit in group?

Edit: I need it for Java language. Example here is juts for getting working regular expression which I test using sed command.

Thank you.

6
  • It’s rather a language that uses POSIX BRE/GNU BRE (since () are escaped). Commented Nov 24, 2010 at 19:10
  • 1
    Friends don't let friends parse HTML with regular expressions. Use an HTML parser instead. Commented Nov 24, 2010 at 19:13
  • 1
    @Ether: Don't be stupid. He's not parsing HTML, he's extracting a number. Friends don't let friends do cargo-cult programming either. Commented Nov 24, 2010 at 19:16
  • 1
    @Mike, if he's extracting a number, he's likely extracting other things as well. Pretty soon it's what one might call parsing. Commented Nov 24, 2010 at 19:23
  • 1
    @Mark: Maybe. But, that's neither here nor there, since the OP is not doing anything like the question @Ether posted. Commented Nov 24, 2010 at 19:26

5 Answers 5

3

Use HTML::TableExtract.

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

Comments

0

In javascript you can do this:

var num = parseInt(someString.replace( /\D/g , ''));

Comments

0

I think you have a slightly peculiar regex implementation. What's the environment?

   s/^[^\d]*\(\d+\)<[^\d]**$/\1/

Has to be worth a go, though. Check whether the set pattern needs [ or [ and if it allows character classes (\d) first. If no character classes 0-9 should do it.

Comments

0

regex (?:<(?:[^>])+>)(\d+)(?:(?:<\/[^>]+)+>) capture only the numbers from your text that are betwen html tags

Comments

0

Although you don't explain what language you're using, the answer is simple.

When you have captured expressions (parenthesis), there are multiple results.

The first one, #0, is always the whole match. Since you have .* before and after the digits, the extra HTML is included in the result.

However, in the second match, #1, you should have only the number. The way to retrieve this result varies depending on the language, but if you update your question, we may be able to help you in that regard.

Edit:

public static String extractNumber(String input) {
    Pattern p = Pattern.compile("s/(\\d+)/");

    Matcher m = p.matcher(input);

    if(m.find()) {
        String num = m.group(1);
        return Integer.parseInt(num);
    }

    return null;
}

This will extract the first number it finds in the input text. And, it demonstrates how to use groups as well.

I haven't tested it since I don't have a proper java environment set up at the moment, but it looks okay. Let me know if you have any problems.

1 Comment

@yart: I've updated my post with a method that should help you out.

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.