gpt4 book ai didi

python 重新: r'\b\$\d+\b' won't match 'aug 12, 2010 abc $123'

转载 作者:行者123 更新时间:2023-11-28 19:59:35 24 4
gpt4 key购买 nike

所以我只是制作一个脚本来从事务日志类型文件中收集 $ 值

for line in sys.stdin:
match = re.match( r'\b \$ (\d+) \b', line)
if match is not None:
for value in match.groups():
print value

现在我只是想打印这些值它会匹配包含 $12323 的行,但当该行中有其他内容时则不会根据我的阅读,它应该可以工作,但看起来我可能会遗漏一些东西

最佳答案

re.match :

If zero or more characters at the beginning of string match this regular expression, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

您正在寻找的是 re.searchre.findall :

#!/usr/bin/env python

import re
s = 'aug 12, 2010 abc $123'

print re.findall(r'\$(\d+)', s)
# => ['123']

print re.search(r'\$(\d+)', s).group()
# => $123

print re.search(r'\$(\d+)', s).group(1)
# => 123

关于 python 重新: r'\b\$\d+\b' won't match 'aug 12, 2010 abc $123' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3728292/

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