[英]How to match “0 0 0” exactly using python regular expression?
I am trying to match all "ALL ZEROs" from the following command output. 我正在尝试从以下命令输出中匹配所有“所有零”。
router-7F2C13#show app stats gmail on TEST/switch1234-15E8CC
--------------------------------------------------------------------------------
APPLICATION BYTES_IN BYTES_OUT NUM_FLOWS
--------------------------------------------------------------------------------
gmail 0 0 0
--------------------------------------------------------------------------------
router-7F2C13#
I need to meet the following requirements: 我需要满足以下要求:
I tried with following code: 我尝试使用以下代码:
x="""router-7F2C13#show app stats gmail on TEST/switch1234-15E8CC
--------------------------------------------------------------------------------
APPLICATION BYTES_IN BYTES_OUT NUM_FLOWS
--------------------------------------------------------------------------------
gmail 0 0 0
--------------------------------------------------------------------------------
router-7F2C13#"""
def pass_fail_criteria(x):
if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+"):
print "FAIL"
else:
print "PASS"
print pass_fail_criteria(x)
But its throws following error: 但是它抛出以下错误:
C:\Users\test\Desktop>python test_regexp.py
File "test_regexp.py", line 17
if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+"):
^
SyntaxError: invalid syntax
C:\Users\test\Desktop>
Can anyone please help me to fix this? 谁能帮我解决这个问题?
Is it possible to match only all zero's with exact spaces? 是否可以仅将所有零与精确的空格匹配?
You might try a regex like 您可以尝试像
\s*\w+\s+(\d+)\s+(\d+)\s+(\d+)
This would seem to match 这似乎匹配
gmail 0 0 0
An alternative (at your request) is to match the literal 0
's. 另一种方法(根据您的要求)是将文字
0
匹配。 Just replace the digit match with 0: 只需将数字匹配替换为0:
\s*\w+\s+(0)\s+(0)\s+(0)
Note that the (parentheses) are for grouping, and may not be necessary depending on how you'll be using the regex. 请注意,(括号)用于分组,根据您使用正则表达式的方式,(括号)可能不是必需的。 If you're just testing for a match, you can drop them.
如果您只是在测试比赛,则可以将其删除。
Then you should probably write a function to ensure the matches are all 0. 然后,您可能应该编写一个函数以确保匹配全为0。
You might take a look at debuggex . 您可以看看debuggex 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.