繁体   English   中英

在 python 中使用正则表达式在冒号或括号后提取字符串

[英]Extract string after colon or parenthesis with regex in python

输入:

Subject: Representation of Territories? (Was: Re: The $11,250,000,000,000 lunch)

output:

Representation of Territories

输入:

Subject: Re: Top Ten Responses to Ed's Top Ten Lists

output:

Top Ten Responses to Ed's Top Ten Lists

您可以使用可选的捕获组来执行此操作:

import re

text_1 = "Subject: Representation of Territories? (Was: Re: The $11,250,000,000,000 lunch)"
text_2 = "Subject: Re: Top Ten Responses to Ed's Top Ten Lists"

regex = re.compile(r"Subject:\s"
                   r"(?:\w{2}:\s)?"  # Optional capture group
                   r"((\w+[']?\w+?\s?)+\w)"
                   r"\??",
                   re.I
                   )

res_1 = re.search(regex, text_1).group(1)
res_2 = re.search(regex, text_2).group(1)

print(res_1)
print(res_2)

返回:

Representation of Territories
Top Ten Responses to Ed's Top Ten Lists

另外,根据评论中的要求:

text_3 = "Subject: Re: F<O>CUS/HEALTH: ONE PAYER SYSTEM B.S."
regex_3 = re.compile(r"(.*:\s)+(.*)")
res_3 = re.search(regex_3, text_3).group(2)
print(res_3)

返回:

ONE PAYER SYSTEM B.S.

暂无
暂无

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

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