我好像找不到关于这个问题的线程,但这应该是相当简单的事情。我正在尝试使用正则表达式在输出中搜索0-99之间的数字,并执行某个操作,但如果数字是100,则会执行不同的操作。这是我尝试过的代码(简化版):
OUTPUT = #Some command that will store the output in variable OUTPUT
OUTPUT = OUTPUT.split('\n')
for line in OUTPUT:
if (re.search(r"Rebuild status: percentage_complete", line)): #searches for the line, regardless of number
if (re.search("\d[0-99]", line)): #if any number between 0 and 99 is found
print("error")
if (re.search("100", line)): #if number 100 is found
print("complete")
我已经尝试过这个,但它仍然捕捉到100并打印错误。
100
在技术上是该正则表达式的一个有效匹配项(它会匹配值10
)。我已经修正了正则表达式,加入了单词边界符(\b
),以解决这个问题。 - npintiif re.search("\d{1,2}", line) and not re.search("100", line):
这似乎有效。感谢您的帮助! - bladexeon