简体   繁体   中英

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

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:

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.

Problems:

1. You should not print whole line.

2. You should use ^From:[^@]+ to prevent matching of @

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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