“ Gokila,希望您过得愉快!再次见到您,恭维券的有效期为自发出之日起6个月。”
答案 0 :(得分:1)
通过按位置编号访问每个组,可以在正则表达式中进行匹配。在我提供的正则表达式中,只有一个捕获括号。
import re
str = "I hope you enjoyed the stay, Gokila!HOPE TO SEE YOU AGAIN Your compliment tickets are valid for 6 months from the date of issue."
matches = re.search(".*\s(.*)\!.*", str)
print(matches.group(1))
# Gokila
答案 1 :(得分:0)
将re.match
与捕获组一起使用:
inp = "I hope you enjoyed the stay, Gokila!HOPE TO SEE YOU AGAIN Your compliment tickets are valid for 6 months from the date of issue."
m = re.match(r'.*,\s+(\w+)\b.*', inp)
if m:
print(m.groups(1)[0])
此打印:
Gokila
答案 2 :(得分:0)
尝试此正则表达式:
(?<=, )(?:\w+\s*)+(?=!)
这是假定名称前逗号后有空格