line = 'I am trying to extract the number from this string REG/123 which is between REG/ and a white space.'
number = re.search(r'(?<=\bREG/)\d+', line).group(0)
print(number)
输出:
123
说明:
(?<= # positive lookbehind, make sure we have before:
\b # a word boundary
REG/ # literally REG/ string
) # end lookbehind
\d+ # 1 or more digits
对于像'REG/123'这样的模式,regex将是
r'^REG/\d+$'
test=re.search(r'^REG/\d+$',line)
获取所有匹配项后,可以运行循环,通过使用
.split("/")[1]
拆分字符串来仅提取数字我最后做的是下面的代码,它对我来说是有效的,我用一个特定的字符替换了空格,然后做了regex。你知道吗
输出:
说明:
相关问题 更多 >
编程相关推荐