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.

Mark Thomas
  • 37,131
  • 11
  • 74
  • 101
yart
  • 7,515
  • 12
  • 37
  • 37
  • It’s rather a language that uses POSIX BRE/GNU BRE (since `()` are escaped). – Gumbo Nov 24 '10 at 19:10
  • Are those parentheses supposed to be escaped? That would prevent the capture from working... – Mike Caron Nov 24 '10 at 19:13
  • 1
    [Friends don't let friends parse HTML with regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). Use an HTML parser instead. – Ether Nov 24 '10 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. – Mike Caron Nov 24 '10 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. – Mark Thomas Nov 24 '10 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. – Mike Caron Nov 24 '10 at 19:26

5 Answers5

3

Use HTML::TableExtract.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
0

In javascript you can do this:

var num = parseInt(someString.replace( /\D/g , ''));
theChrisKent
  • 15,029
  • 3
  • 61
  • 62
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.

Mike Caron
  • 14,351
  • 4
  • 49
  • 77
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.

The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
0

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

cristian
  • 8,676
  • 3
  • 38
  • 44