简体   繁体   English

使用贪婪运算符的Python正则表达式搜索

[英]Python regex search with greedy operator

I am trying to use regex to find an expression, and here is what i am trying to do 我正在尝试使用正则表达式来查找表达式,这就是我正在尝试做的

import re
h1= open("test.txt")
for ln in h1:
    ln = ln.rstrip()
    if re.search("^From:.+@", ln):
        print(ln)

As per the logic it should stop the search at '@', however in my case that doesn't happen i get the following output: From: ray@media.berkeley.edu 按照逻辑,它应该在“ @”处停止搜索,但是在我没有发生的情况下,我得到以下输出:从:ray@media.berkeley.edu

I also tried to use a qualifier '?' 我也尝试使用限定词'?' but it doesn't seem to work. 但它似乎不起作用。 Can someone suggest why it isn't working or how can i make it work specifically using regex. 有人可以建议为什么它不起作用,或者我如何使用正则表达式使它特别起作用。

您正在打印整行,而可以执行以下操作:

print(ln.split('@')[0])

Regex solution: 正则表达式解决方案:

# 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)))

Other way by splitting and using startswith: 通过拆分和使用startswith的其他方法:

h1= open("test.txt")
for ln in h1:
    if ln.startswith("From: "):
        print(ln.split("@")[0])

# Output From: ray

If Regex is the only thing which you are looking for, Then you can try something like this. 如果正则Regex是您唯一需要的东西,那么您可以尝试这样的事情。

Problems: 问题:

1. You should not print whole line. 1.您不应打印整行。

2. You should use ^From:[^@]+ to prevent matching of @ 2.您应使用^From:[^@]+来防止匹配@

Try this code snippet here 在此处尝试此代码段

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))

Try regex demo here 在这里尝试正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM