[英]Python regex search with greedy operator
我正在尝试使用正则表达式来查找表达式,这就是我正在尝试做的
import re
h1= open("test.txt")
for ln in h1:
ln = ln.rstrip()
if re.search("^From:.+@", ln):
print(ln)
按照逻辑,它应该在“ @”处停止搜索,但是在我没有发生的情况下,我得到以下输出:从:ray@media.berkeley.edu
我也尝试使用限定词'?' 但它似乎不起作用。 有人可以建议为什么它不起作用,或者我如何使用正则表达式使它特别起作用。
您正在打印整行,而可以执行以下操作:
print(ln.split('@')[0])
正则表达式解决方案:
# regex = r"(.+?)@"
import re
h1= open("test.txt")
regex = r"(.+?)@"
for ln in h1:
ln = ln.rstrip()
matches = re.search(regex, ln)
if matches:
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum)))
通过拆分和使用startswith的其他方法:
h1= open("test.txt")
for ln in h1:
if ln.startswith("From: "):
print(ln.split("@")[0])
# Output From: ray
如果正则Regex
是您唯一需要的东西,那么您可以尝试这样的事情。
问题:
1.您不应打印整行。
2.您应使用
^From:[^@]+
来防止匹配@
import re
h1= open("test.txt")
for ln in h1:
ln = ln.rstrip()
result = re.search("^From:[^@]+", ln)
if result is not None:
print(result.group(0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.