简体   繁体   English

如何编写正则表达式模式以匹配字符串结尾或字符串开头的货币符号

[英]How to write a regex pattern to match string with currency sybol at end OR start of string

I'm trying to write a regex pattern in Python that will only match the substrings which have a currency symbol/word attached.我正在尝试在 Python 中编写一个正则表达式模式,该模式仅匹配带有货币符号/单词的子字符串。

String example:字符串示例:

'50,000 and £200.6m, 50p, 500m euro, 800bn euros, $15bn and $99.99. The year 2006 is 20% larger.' 

Expected matches:预期匹配:

  1. £200.6m 2.006 亿英镑
  2. 50p 50便士
  3. 500m euro 5亿欧元
  4. 800bn euros 8000亿欧元
  5. $15bn 150 亿美元
  6. $99.99 99.99 美元

I do not want numbers that are not currency related to match, such as plain numbers, percentages or years.我不想要与匹配货币无关的数字,例如纯数字、百分比或年份。

My attempt:我的尝试:

(^(£|\$)?)\d+([.,]\d*)?|(\d+([.,]\d*)?(p|(bn|m)|(\seuro(s)))$)

My matches:我的比赛:

  1. 50,000 50,000

Evidently the regex isn't working at all as I expect.显然,正则表达式根本不像我预期的那样工作。 It should either match a substring if it begins with a currency symbol or if it ends with one.如果它以货币符号开头或以 1 结尾,则它应该与 substring 匹配。

You might use a pattern to match either the euro or dollar sign [£$] , the value optionally followed by p m or bn .您可以使用模式来匹配欧元或美元符号[£$] ,该值可选地后跟p mbn

Or match the value followed by p m or bn and euro with optional s或者将后跟p mbn和 euro 的值与可选的 s 匹配

(?:[£$]\d+(?:\.\d+)?(?:[pm]|bn)?|\d+(?:\.\d+)?(?:[pm]|bn)(?: euros?)?)

Explanation解释

  • (?: Non capture group (?:非捕获组
    • [£$] A character class matching either £ or $ [£$]匹配£$的字符 class
    • \d+(?:\.\d+)? Match 1+ digits with an optional decimal part将 1+ 位数字与可选的小数部分匹配
    • (?:[pm]|bn)? Optionally match either p or m or bn可选择匹配pmbn
    • | Or或者
    • \d+(?:\.\d+)? Match 1+ digits with optional decimal part匹配 1+ 位可选小数部分
    • (?:[pm]|bn) Match either p or m or bn (?:[pm]|bn)匹配pmbn
    • (?: euros?)? Optionally match a space and euro with optional s可选地将空格和euro与可选s匹配
  • ) Close group )关闭组

Regex demo正则表达式演示

Something like this would do it:这样的事情会做到这一点:

(?:£|\$)(?:\d*\.)?\d+(?:m|bn)?|(?:\d*\.)?\d+(?:m|bn)? ?(?:p|euros?)

Just keep adding the currencies you wish to catch to the respective (?:£|\$) or (?:p|euros?) sections.只需继续将您希望捕获的货币添加到相应的(?:£|\$)(?:p|euros?)部分。 Ditto for adding items to (?:m|bn)同上添加项目到(?:m|bn)

https://regex101.com/r/3K9jmR/1 https://regex101.com/r/3K9jmR/1

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

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