简体   繁体   中英

How to get the last word(s) in a paragraph in Python using regex

I am looking for a way to extract the last word in a line. I only want to extract the First name: Mike My code is

import re

text_to_search = '''
I like Apples and bananas 
I like fruits and yogurt
thisUser: Your name : Mike Lewis
Email: mike@mail.com
type: Fullresopnse
'''
pattern = re.compile(r'thisUser: Your name :\s[A-Z]\w+')

matches = pattern.search(text_to_search)

print(matches)

running this code gets me :

re.Match object; span=(54, 80), match='thisUser: Your name : Mike'

How do I get only "Mike" or "Mike lewis" to print?

You could try using re.findall here:

matches = re.findall(r'\bYour name\s*:\s*(\S+)\s+(\S+)', text_to_search)
print("first name: " + matches[0][0])
print("last name: " + matches[0][1])

This prints:

first name: Mike
last name: Lewis

The potential advantage of re.findall here might be if you expect your text to have more than one name entry in it.

This expression has a capturing group that would return Mike:

thisUser:\s*Your name\s*:\s*(\S+)

Demo

Test

import re

regex = r"thisUser:\s*Your name\s*:\s*(\S+)"

test_str = ("I like Apples and bananas \n"
    "I like fruits and yogurt\n"
    "thisUser: Your name : Mike Lewis\n"
    "Email: mike@mail.com\n"
    "type: Fullresopnse")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

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