如何编写正则表达式可以1.
匹配可能包含或不包含逗号或小数的货币,2.
仅匹配货币代码。我看到的大多是匹配的货币符号。 我希望能够从一个完整的文本相匹配的货币['300,000.00']
和货币代码['USD']
像这样:Python正则表达式匹配有或没有逗号或小数的货币
Userid 9XXXX219 sales USD300,000.00 On 01-JUL-2016 08:34:32
到目前为止,我试过,但它仅与十进制的,而不是那些没有小数点匹配或有逗号的人:
s = 'USD1 USD1.00 USD100.00 USD1,000 CAD1,000.00'
re.findall(r'\d+\.\d+', s)
#matches
['1.00', '100.00', '000.00']
#should not match any other thing e.g. 1XXXX324
#instead of this:
['1','1.00', '100.00', '1,000', '1,000.00']
如何编写另一个正则表达式模式来匹配仅货币代码?即
['USD', 'USD', 'USD', 'USD','CAD']
're.compile(“(\ d +(? :[\。,] \ d +)?)')'工作 - 检查出来! – wolframalpha
@wolframalpha匹配有或没有小数,但不匹配逗号,如'1,000.00' – DougKruger
're.compile('(\ d +(?:[\。,] \ d +)*)')'如果您想要多个逗号和十进制点! – wolframalpha