使用regex在字符串开头提取USD amout

时间:2022-09-13 11:41:24

Need help to extract the usd amount at beginning of string.

需要帮助提取字符串开头的美元金额。

Example

例子

String : $50,000.00 NAMED INSURED FOR $100K/$300K LIMITS
Output : 50,000

字符串:$50,000.00以$100K/$300K限制输出命名保险:50,000

This is what I tried \$\d.*?000\.00 but I am sure there is a better way.

这就是我用的\$\d.*?000\。但是我肯定有更好的办法。

3 个解决方案

#1


2  

^\$?([\d,\.]+)\b

Or something of the sort. Broken down:

或者类似的东西。分解:

^            # anchor to start of line
\$?          # look for dollar sign (but optional)
(            # begin capture group
  [\d,\.]+   # match 1 or more of the following: `0-9`, `,` or `.`
)            # end capture group
\b           # word boundary

Example of the above can be found here: http://regexr.com?372t4

上面的示例可以在这里找到:http://regexr.com?372t4

note: if you're looking to convert this in to a double/float value in a language, you're going to need a parser that ignores commas or do some simple string manipulation to remove them.

注意:如果您想要将它转换为语言中的双/浮点值,您需要一个忽略逗号的解析器,或者做一些简单的字符串操作来删除它们。

#2


1  

Try this regular expression:

试试这个正则表达式:

\$(\S+)

which will give you 50,000.00 -- the exact amount.

这将给你50,000.00——确切的数额。

Find the $ sign and then match all non-whitespace characters.

找到$符号,然后匹配所有非空格字符。

#3


0  

Try:

试一试:

^\$(\d{1,3}(?=,\d{3})*.\d{2})

#1


2  

^\$?([\d,\.]+)\b

Or something of the sort. Broken down:

或者类似的东西。分解:

^            # anchor to start of line
\$?          # look for dollar sign (but optional)
(            # begin capture group
  [\d,\.]+   # match 1 or more of the following: `0-9`, `,` or `.`
)            # end capture group
\b           # word boundary

Example of the above can be found here: http://regexr.com?372t4

上面的示例可以在这里找到:http://regexr.com?372t4

note: if you're looking to convert this in to a double/float value in a language, you're going to need a parser that ignores commas or do some simple string manipulation to remove them.

注意:如果您想要将它转换为语言中的双/浮点值,您需要一个忽略逗号的解析器,或者做一些简单的字符串操作来删除它们。

#2


1  

Try this regular expression:

试试这个正则表达式:

\$(\S+)

which will give you 50,000.00 -- the exact amount.

这将给你50,000.00——确切的数额。

Find the $ sign and then match all non-whitespace characters.

找到$符号,然后匹配所有非空格字符。

#3


0  

Try:

试一试:

^\$(\d{1,3}(?=,\d{3})*.\d{2})