2

I want to get 116.83 and 81.16 from the below string. I tried \d but it also selects 2 from up2 and dn2. How can i ignore this.

<td align="right">
    116.83<span class="up2"></span><br>81.16<span class="dn2"></span>
</td>
7
  • What characters are allowed to come before and after the number? Commented Apr 6, 2013 at 6:44
  • if you want to parse HTML, don't use regular expressions, use a DOM parser. Commented Apr 6, 2013 at 6:45
  • What language? Why not use a proper parser? Commented Apr 6, 2013 at 6:45
  • The value is out of span tag. How can i parse the data. Commented Apr 6, 2013 at 6:46
  • 1
    In the javascript DOM API, that is nodes = [].filter.call(elem.childNodes, function(x){return x.nodeType==3}) Commented Apr 6, 2013 at 6:50

2 Answers 2

5
\b[\d.]+\b

\b matches the boundary between word and non-word characters, but doesn't include the adjacent characters in the match. Since letters and numbers are both word characters, it won't match between p and 2, so up2 doesn't match. But > is a non-word character, so it matches between > and 8, therefore the regexp matches 81.16.

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

4 Comments

Its what i wanted. Thanks you very much. Can you also explain. I have accepted/voted your answer.
I did explain. What part don't you understand?
@Barmar - Did you mean to escape the period? \b[\d\.]+\b
@Aiias No, period has no special meaning inside [], so it doesn't need to be escaped.
1

Try something like this:

[>\s]+([\d\.]+)[<\s]+

But be sure to strip the leading > and whitespace as well as the trailing < and whitespace from the first level match OR take the second level matches only.

1 Comment

You should be using the value from capture group 1, which gets what's in the parentheses.

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.