问题描述
我如何从以下字符串中提取美元金额
How would I extract the dollar amount from the following string
一些文本会在这里越来越多,然后会有一些价格$ 34.03,但这并不意味着字符串将结束"
"some text will go here and more and more and then there will be some price $34.03 but that doesn't mean the string will end"
我想提取$ 34.03 ...也想提取没有美分的东西
I want to extract $34.03...also want to extract if there is no cents
一些文本会越来越多地出现在这里,然后会有一些价格$ 34,但这并不意味着字符串将结束"
"some text will go here and more and more and then there will be some price $34 but that doesn't mean the string will end"
在这里我要提取$ 34
Here I want to extract $34
推荐答案
我不是regex专家,但是可以使用 RegExr .
I'm no regex-guru, but was able to whip up the following with RegExr.
/(\$[0-9]+(\.[0-9]{2})?)/
匹配$35.03
和$35
.要接受$35,000.52
之类的格式,您需要包含,
Matches $35.03
and $35
. To accept formats like $35,000.52
you would need to include ,
/(\$[0-9,]+(\.[0-9]{2})?)/
这可能会有所改善,但是从我的初步测试来看,它工作得很好.
This could likely be improved upon, but from my preliminary tests, it works just fine.
这篇关于RegEx-如何提取价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!