如何使用正则表达式在Python中获取段落中的最后一个单词

时间:2019-06-28 05:20:23

标签: python regex

我正在寻找一种方法来提取一行中的最后一个单词。我只想提取名字:Mike 我的代码是

timeit

运行此代码会让我:

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)

如何仅打印re.Match object; span=(54, 80), match='thisUser: Your name : Mike' "Mike"

2 个答案:

答案 0 :(得分:1)

您可以在此处尝试使用re.findall

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

此打印:

first name: Mike
last name: Lewis

re.findall在这里的潜在优势可能是,如果您希望文本中包含多个名称条目。

答案 1 :(得分:1)

此表达式具有一个捕获组,该捕获组将返回Mike:

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

Demo

测试

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