Santosh
Santosh

Reputation: 12353

Regex get only numeric values from string

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>

Upvotes: 2

Views: 1504

Answers (2)

Barmar
Barmar

Reputation: 782564

\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.

Upvotes: 5

Aiias
Aiias

Reputation: 4748

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.

Upvotes: 1

Related Questions