Python:如何对正则表达式应用两个异常?

Python:如何对正则表达式应用两个异常?,python,python-3.x,regex,Python,Python 3.x,Regex,我有一个正则表达式代码用于查找异常模式 if not re.match(r'(-?[01]\.[0-9]{1,8})\s(-?[01]\.[0-9]{1,8})', text): print("anomaly!!") 我想通过找到异常模式的东西,如果不是 我的代码通常运行良好,但我发现有一种情况不起作用: 0.00000000e+00 // It should be error (included non-numeric strings) 0.000000 //

我有一个正则表达式代码用于查找异常模式

if not re.match(r'(-?[01]\.[0-9]{1,8})\s(-?[01]\.[0-9]{1,8})', text):
    print("anomaly!!")
我想通过
找到异常模式的东西,如果不是

我的代码通常运行良好,但我发现有一种情况不起作用:

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)
我遵守了以下规则:

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.
我认为,我的正则表达式无法检测完全零(0.0到0.00000000)和非数值

如何将两个异常应用于正则表达式

请给我一些建议

这是我的测试用例:

[-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.

在模式的末尾添加一个
$
字符。否则,re.match()函数将接受以模式开头的任何字符串

例如:

print(re.match(r'a', 'abc'))
print(re.match(r'a$', 'abc'))
结果:

   <re.Match object; span=(0, 1), match='a'>
   None

没有一个
就你而言:

>>> 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'>
>>打印(重新匹配(r'([01]\[0-9]{1,8})\s(-[01]\[0-9]{1,8})$,'1.16797414 0.00000000 E+00'))
没有一个
>>>打印(重新匹配(r'([01]\[0-9]{1,8})\s(-[01]\[0-9]{1,8})'1.16797414 0.00000000 E+00'))

(这不会过滤掉“精确零”的情况。您需要在结尾处强制输入一个非零数字,例如
[0-9]{0,7}[1-9]
(注意这将过滤掉0.0000但也过滤掉0.50000),或者您需要在之后检查匹配数字的值。)

在模式的结尾添加一个
$
字符。否则,re.match()函数将接受以模式开头的任何字符串

例如:

print(re.match(r'a', 'abc'))
print(re.match(r'a$', 'abc'))
结果:

   <re.Match object; span=(0, 1), match='a'>
   None

没有一个
就你而言:

>>> 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'>
>>打印(重新匹配(r'([01]\[0-9]{1,8})\s(-[01]\[0-9]{1,8})$,'1.16797414 0.00000000 E+00'))
没有一个
>>>打印(重新匹配(r'([01]\[0-9]{1,8})\s(-[01]\[0-9]{1,8})'1.16797414 0.00000000 E+00'))

(这不会过滤掉“精确零”的情况。您需要在末尾强制输入一个非零数字,例如
[0-9]{0,7}[1-9]
(注意这将过滤掉0.0000但也过滤掉0.50000),或者您需要在之后检查匹配数字的值。)

您需要使用
重新搜索
(自)并使用以下正则表达式:

\[((?!0(?:\.0+)\s)[01](?:\[0-9]{1,8})\s+(?!0(?:\.0+))[01](?:\[0-9]{1,8})]

如果任何一个数字都是零,则
(?!0(?\.0+)?\s)
(?!0(?\.0+))
查找头将取消匹配

见:

重新导入
n=r'[01](?:\[0-9]{1,8})'#数字匹配部分声明为变量
rx=re.compile(fr“\[((?!0(?:\.0+)?{n}\s))\s+((?!0(?:\.0+){n})])
测试标准=[“[-0.19666128-0.0000],”[-1.09666128-0.16812956],“[-0.180045-0.22017317],“[1.0000786-0.248556552],“[0.1766060-1.]”,“[1.16797414 0.00000000 E+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]"]
对于测试文档中的文本:
如果接收搜索(文本):
打印(f'{text}:有效')
其他:
打印(f{text}:无效)
输出:

[-0.19666128-0.0000]:无效
[-1.09666128-0.16812956]:有效
[-0.180045-0.22017317]:有效
[1.00000786-0.24855652]:有效
[0.1766060-1.]:无效
[1.16797414 0.00000000 E+00]:无效
[-0.0.]:无效
[1.1223297-0.2840327]:有效
[1.-0.]:无效
[-0.11070672-0.20553467]:有效
[1.04924586-0.16772696][0.06169098-0.15855075]:有效
[-0.11988816 1.2051293]:有效
[-0.180045-1.22017317]:有效
[-0.18486786-0.24855652]:有效

您需要使用
re.search
(自)并使用以下正则表达式:

\[((?!0(?:\.0+)\s)[01](?:\[0-9]{1,8})\s+(?!0(?:\.0+))[01](?:\[0-9]{1,8})]

如果任何一个数字都是零,则
(?!0(?\.0+)?\s)
(?!0(?\.0+))
查找头将取消匹配

见:

重新导入
n=r'[01](?:\[0-9]{1,8})'#数字匹配部分声明为变量
rx=re.compile(fr“\[((?!0(?:\.0+)?{n}\s))\s+((?!0(?:\.0+){n})])
测试标准=[“[-0.19666128-0.0000],”[-1.09666128-0.16812956],“[-0.180045-0.22017317],“[1.0000786-0.248556552],“[0.1766060-1.]”,“[1.16797414 0.00000000 E+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]"]
对于测试文档中的文本:
如果接收搜索(文本):
打印(f'{text}:有效')
其他:
打印(f{text}:无效)
输出:

[-0.19666128-0.0000]:无效
[-1.09666128-0.16812956]:有效
[-0.180045-0.22017317]:有效
[1.00000786-0.24855652]:有效
[0.1766060-1.]:无效
[1.16797414 0.00000000 E+00]:无效
[-0.0.]:无效
[1.1223297-0.2840327]:有效
[1.-0.]:无效
[-0.11070672-0.20553467]:有效
[1.04924586-0.16772696][0.06169098-0.15855075]:有效
[-0.11988816 1.2051293]:有效
[-0.180045-1.22017317]:有效
[-0.18486786-0.24855652]:有效

试试
^((?!0(?:\.0+)\s)[01](?:\.[0-9]{1,8})\s+(?!0(?:\.0+))[01](?:\.[0-9]{1,8})$
@WiktorStribi\380;我试图使用这个方法,但它检测不到任何东西。请参阅,与
re.search
一起使用,因为
re.match
仅搜索字符串开头的匹配项。用搜索替换匹配项。match只检查表达式try
^((?!0(?:\.0+)\s)[01](?:\.[0-9]{1,8})\s+(?!0(?:\.0+)$)[01](?:\.[0-9]{1,8})$
@WiktorStribi\380ew我试图使用此方法,但它检测不到任何东西。请参阅,与
re.search
一起使用,因为
re.match
仅搜索字符串开头的匹配项。用搜索替换匹配项。比赛只会检查第一次出现的表情谢谢你这么多!!!!!非常感谢!!!!!