[英]Python: How can I apply two exceptions to my regular expression?
I have this regular expression code for finding the anomaly pattern.我有这个用于查找异常模式的正则表达式代码。
if not re.match(r'(-?[01]\.[0-9]{1,8})\s(-?[01]\.[0-9]{1,8})', text):
print("anomaly!!")
I want to find something that anomaly pattern through if not
.我想通过if not
找到异常模式的东西。
My code usually works well, but I found a case where doesn't work:我的代码通常运行良好,但我发现了一个不起作用的情况:
0.00000000e+00 // It should be error (included non-numeric strings)
0.000000 // It should be error (complete zero cannot exist)
0.00 0000 // It should be error (included non-numeric strings)
I complied with the following rules:我遵守了以下规则:
1. The value after the decimal point can be from at least one letter to at maximum eight.
2. Never include non-numeric strings.
3. A complete zero (0.0 ~0.00000000) cannot exist.
4. A value must exist after the decimal point.
I think, my regular expression can't detect complete zero (0.0 to 0.00000000) and non-numeric values.我认为,我的正则表达式无法检测到完全零(0.0 到 0.00000000)和非数字值。
How can I apply two exceptions to my regular expression?如何对我的正则表达式应用两个例外?
Please give me some advice.请给我一些建议。
This is my test cases:这是我的测试用例:
[-0.19666128 -0.0000] # It should be error (complete zero cannot exist)
[-1.09666128 -0.16812956] # It should be correct.
[-0.180045 -0.22017317] # It should be correct.
[1.00000786 -0.24855652] # It should be correct.
[0.1766060 -1.] # It should be error (A value must exist after the decimal point)
[1.16797414 0.00000000e+00] # It should be error (included non-numeric strings)
[-0. 0.] # It should be error (A value must exist after the decimal point)
[1.1223297 -0.2840327] # It should be correct.
[1. -0. ] # It should be error (A value must exist after the decimal point and included non-numeric strings)
[-0.11070672 -0.20553467] # It should be correct.
[1.04924586 -0.16772696] # It should be correct.
[0.06169098 -0.15855075] # It should be correct.
[-0.11988816 1.20512903] # It should be correct.
[-0.180045 -1.22017317] # It should be correct.
[-0.18486786 -0.24855652] # It should be correct.
Add a $
character to the end of the pattern.在模式的末尾添加一个$
字符。 Otherwise the re.match() function will accept any string that begins with the pattern.否则 re.match() function 将接受以该模式开头的任何字符串。
example:例子:
print(re.match(r'a', 'abc'))
print(re.match(r'a$', 'abc'))
result:结果:
<re.Match object; span=(0, 1), match='a'>
None
In your case:在你的情况下:
>>> print(re.match(r'(-?[01]\.[0-9]{1,8})\s(-?[01]\.[0-9]{1,8})$', '1.16797414 0.00000000e+00'))
None
>>> print(re.match(r'(-?[01]\.[0-9]{1,8})\s(-?[01]\.[0-9]{1,8})', '1.16797414 0.00000000e+00'))
<re.Match object; span=(0, 21), match='1.16797414 0.00000000'>
(This will not filter out the "exact zero" case. You will need to either force a non-zero digit at the end eg [0-9]{0,7}[1-9]
(note this will filter out 0.0000 but also 0.50000), or you will need to check the value of the matched number afterward.) (这不会过滤掉“精确零”的情况。您需要在末尾强制一个非零数字,例如[0-9]{0,7}[1-9]
(注意这将过滤掉 0.0000但也可以是 0.50000),否则您需要在之后检查匹配号码的值。)
You need to use re.search
(since ) and use the following regex:您需要使用re.search
(since ) 并使用以下正则表达式:
\[(-?(?!0(?:\.0+)?\s)[01](?:\.[0-9]{1,8})?)\s+(-?(?!0(?:\.0+)?])[01](?:\.[0-9]{1,8})?)]
See the regex demo .请参阅正则表达式演示。
The (??0(:.\?0+)?\s)
and (??0(:.\?0+)?])
lookaheads will cancel the match if either of the numbers are all zeros.如果其中一个数字全为零,则(??0(:.\?0+)?\s)
和(??0(:.\?0+)?])
前瞻将取消匹配。
See the Python demo :请参阅Python 演示:
import re
n = r'[01](?:\.[0-9]{1,8})?' # Number matching part declared as a variable
rx = re.compile(fr"\[(-?(?!0(?:\.0+)?{n}\s))\s+(-?(?!0(?:\.0+)?]){n})]")
test_strs = ["[-0.19666128 -0.0000]","[-1.09666128 -0.16812956]","[-0.180045 -0.22017317]", "[1.00000786 -0.24855652]", "[0.1766060 -1.]", "[1.16797414 0.00000000e+00]",
"[-0. 0.]", "[1.1223297 -0.2840327]","[1. -0. ]", "[-0.11070672 -0.20553467]","[1.04924586 -0.16772696]"
"[0.06169098 -0.15855075]","[-0.11988816 1.20512903]","[-0.180045 -1.22017317]","[-0.18486786 -0.24855652]"]
for text in test_strs:
if rx.search(text):
print(f'{text}: Valid')
else:
print(f'{text}: Invalid')
Output: Output:
[-0.19666128 -0.0000]: Invalid
[-1.09666128 -0.16812956]: Valid
[-0.180045 -0.22017317]: Valid
[1.00000786 -0.24855652]: Valid
[0.1766060 -1.]: Invalid
[1.16797414 0.00000000e+00]: Invalid
[-0. 0.]: Invalid
[1.1223297 -0.2840327]: Valid
[1. -0. ]: Invalid
[-0.11070672 -0.20553467]: Valid
[1.04924586 -0.16772696][0.06169098 -0.15855075]: Valid
[-0.11988816 1.20512903]: Valid
[-0.180045 -1.22017317]: Valid
[-0.18486786 -0.24855652]: Valid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.