简体   繁体   English

正则表达式仅从字符串中获取数值

[英]Regex get only numeric values from string

I want to get 116.83 and 81.16 from the below string. 我想从下面的字符串中获取116.8381.16 I tried \\d but it also selects 2 from up2 and dn2 . 我试过\\ d但它也从UP2DN2选择2。 How can i ignore this. 我怎么能忽略这一点。

<td align="right">
    116.83<span class="up2"></span><br>81.16<span class="dn2"></span>
</td>
\b[\d.]+\b

\\b matches the boundary between word and non-word characters, but doesn't include the adjacent characters in the match. \\b匹配单词和非单词字符之间的边界,但不包括匹配项中的相邻字符。 Since letters and numbers are both word characters, it won't match between p and 2 , so up2 doesn't match. 由于字母和数字都是单词字符,因此p2之间不匹配,因此up2不匹配。 But > is a non-word character, so it matches between > and 8 , therefore the regexp matches 81.16 . 但是>是非单词字符,因此它在>8之间匹配,因此regexp与81.16匹配。

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. 但是请确保从第一级匹配中删除前导>和空格以及尾随<和空白,或者仅获取第二级匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM