[英]Python, Matching Regular Expressions
I'm practicing regular expression with python using regex101.com.我正在使用 regex101.com 练习 python 的正则表达式。 But i'm having trouble trying to meet these conditions: A valid string starts with (1) a positive + or negative - symbol, directly followed by (2) a integer, followed by (3) one or more spaces, followed by (4) one of the following operators: + - * / or ˆ, followed by (5) one or more spaces, followed by (6) a positive or negative symbol, directly followed by (7) an integer.
但是我在尝试满足这些条件时遇到了麻烦:有效的字符串以 (1) 一个正 + 或负 - 符号开头,后跟 (2) integer,然后是 (3) 一个或多个空格,然后是 ( 4) 以下运算符之一:+ - * / 或 ^,后跟 (5) 一个或多个空格,后跟 (6) 正负符号,直接后跟 (7) integer。
Match:
+6 * -2
+99 / +15
-100 ^ -2
-13 - -33
+12 - +102
+3 + +3
-3 + -3
Not a Match:
- 15/-12
-19 + 12
+12 - +102
3 + 3
What i have written below, meets both what's matched and not matched:我在下面写的内容符合匹配和不匹配的内容:
r'^[-+]?.|[0-9]|[ ]{1,}[+,-,*,/,^]|[ ]{1,}[-+]|[0-9].$'
anyone sees the issue?有人看到这个问题吗? greatly appreciated.
非常感激。
I would use:我会使用:
[+-]\d+\s+[*/^+-]\s+[+-]\d+
Sample script:示例脚本:
inp = """Match:
+6 * -2
+99 / +15
-100 ^ -2
-13 - -33
+12 - +102
+3 + +3
-3 + -3
Not a Match:
- 15/-12
-19 + 12
+12 - +102
3 + 3"""
matches = re.findall(r'[+-]?\d+\s+[*/^+-]\s+[+-]?\d+', inp)
print(matches)
This prints:这打印:
['+6 * -2', '+99 / +15', '-100 ^ -2', '-13 - -33', '+12 - +102', '+3 + +3', '-3 + -3',
'+12 - +102']
Note that this actually includes +12 - +102
;请注意,这实际上包括
+12 - +102
; there does not seem to be anything invalid about this sample, despite that you listed it in the non match set.尽管您将其列在非匹配集中,但该样本似乎没有任何无效之处。
Here is an explanation of the regex pattern:以下是正则表达式模式的解释:
[+-] match a leading + or - in front of each number operand
\d+ match one or more digits
\s+ one or more spaces (technically any whitespace character)
[*/^+-] an operator
\s+ one or more spaces
[+-] leading + or -
\d+ one or more digits
You put the alternative symbol ( |
) between your terms, where it shouldn't be.您将替代符号 (
|
) 放在不应该出现的位置之间。 Besides, you put commas within a character class [+,-,*,/,^]
- this simply means that comma is also one of matched characters.此外,您将逗号放在字符 class
[+,-,*,/,^]
中 - 这仅表示逗号也是匹配字符之一。 Also note that within this class: [+*/^-]
order is somewhat important - -
must come first or last, otherwise it is interpreted as a character range, while ^
can't be first or it is interpreted as negation.另请注意,在此 class 中:
[+*/^-]
顺序有些重要 - -
必须先或最后,否则将被解释为字符范围,而^
不能是第一个或被解释为否定。 The correct regexp fitting your criteria is:适合您的标准的正确正则表达式是:
r'^[-+][0-9]+[ ]+[+*/^-][ ]+[-+][0-9]+$'
and it can be further shortened by replacing [0-9]
with \d
.并且可以通过将
[0-9]
替换为\d
来进一步缩短它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.