gpt4 book ai didi

Python 正则表达式搜索数字范围

转载 作者:太空狗 更新时间:2023-10-30 00:49:29 26 4
gpt4 key购买 nike

我似乎找不到关于这个的话题,但看起来应该很简单。我正在尝试使用正则表达式在输出中的一行中搜索数字 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 并打印错误。

最佳答案

这:\d[0-99] 表示一个数字(\d),后跟一个数字(0-9)或 9。如果您在 [0-99] 的数字范围之后,则需要使用类似于 \b\d{1,2}\b 的内容。这将匹配任何由 1 位或 2 位数字组成的数值。

关于Python 正则表达式搜索数字范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29629962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com