RegEx-如何提取价格? [英] RegEx - How to Extract Price?

查看:73
本文介绍了RegEx-如何提取价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从以下字符串中提取美元金额

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-如何提取价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆