简体   繁体   English

匹配可变项方程

[英]Matching variable-term equations

I am trying to develop a regular expression to match the following equations: 我正在尝试开发一个正则表达式以匹配以下方程式:

(Price+10%+100+200)
(Price+20%+200)
(Price+30%)
(Price+100)

(Price-10%-100-200)
(Price-20%-200)
(Price-30%)
(Price-100)

My regex so far is... 到目前为止,我的正则表达式是...

/([(])+([P])+([r])+([i])+([c])+([e])+([+]|[-]){1}([\d])+([+]|[-])?([\d])+([%])?([)])/g

..., but it only matches the following equations: ...,但仅与以下等式匹配:

(Price+100+10%)
(Price+100+100)
(Price+200)
(Price-100-10%)
(Price-100-100)
(Price-200)

Can someone help me understand how to make my pattern match the full set of equations provided? 有人可以帮助我了解如何使我的模式与提供的所有方程式匹配吗?

Note: Parentheses and 'Price' are musts in the equations that the pattern must match. 注意:括号和“价格”是模式必须匹配的等式中的必填项。

Try this, which matches all the input strings provided in the question: 试试这个,它匹配问题中提供的所有输入字符串:

/\(Price([+-]\d+%?){1,3}\)/g

You can test it in a regex fiddle . 您可以在正则表达式小提琴中对其进行测试。

Things to note: 注意事项:

  • Only use parentheses where you want to group. 仅在要分组的地方使用括号。 Parentheses around single-possibility, fixed-quantity matches (eg ([P]) provide no value. 围绕单可能性,固定数量匹配项(例如([P])括号不提供任何值。
  • Use character classes (opened with [ and closed with ] ) for multiple characters that can match at a position in the pattern (eg [+-] ). 对于可以在模式中某个位置匹配的多个字符(例如[+-] ),请使用字符类(以[打开]和[]结束)。 Single-possibility character classes (eg [P] ) similarly provide no value. 类似地,单可能性字符类(例如[P] )不提供任何值。
  • Yes, character classes (generally) implicitly escape regex special characters within them (eg ( in [(] vs. equivalent \\( outside a character class), but to just escape regex special characters (ie to match them literally), you are better off not using a character class and just escaping them (eg \\( ) – unless multiple characters should match at a position in the pattern (per the previous point to note). 是的,字符类(通常)会隐式地将其中的正则表达式特殊字符转义(例如([(] vs. \\(字符类之外,等同于\\(字符类之外)),但是转义正则表达式特殊字符(即,按字面意义进行匹配),不要使用字符类,而只是转义字符(例如\\( )–除非模式中的某个位置应匹配多个字符(根据前一点要注意)。
  • The quantifier {1} is (almost) always useless: drop it. 量词{1}几乎总是无用的:将其删除。
  • The quantifier + means "one or more" as you probably know. 如您所知,量词+表示“一个或多个”。 However, in a series of cases where you used it (ie ([(])+([P])+([r])+([i])+([c])+([e])+ ), it would match many values that I doubt you expect (eg ((((((PPPrriiiicccceeeeee ): basically, don't overuse it. Stop to consider whether you really want to match one or more of the character (class) or group to which + applies in the pattern. 但是,在使用它的一系列情况下(即([(])+([P])+([r])+([i])+([c])+([e])+ ) ,它将匹配我怀疑您期望的许多值(例如((((((PPPrriiiicccceeeeee ):基本上,不要过度使用它。停止考虑您是否真的要匹配一个或多个字符(类)或组)模式中的+适用于。
  • To match a literal string without any regex special characters like Price , just use the literal string at the appropriate position in the pattern – eg Price in \\(Price . 要匹配没有任何正则表达式特殊字符(如Price的文字字符串,只需在模式中的适当位置使用文字字符串,例如Price in \\(Price

/\\(Price[+-](\\d)+(%)?([+-]\\d+%?)?([+-]\\d+%?)?\\)/g

可在http://www.regexr.com/上使用

/^[(Price]+\d+\d+([%]|[)])&/i

请您自担风险!

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

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