5 回答
TA贡献1833条经验 获得超4个赞
这也可以,而且很简单
str = "review: I love you very much... reviewer:jackson review: I hate you very much... reviewer:madden review: sky is pink and i ... reviewer: tom"
matches = re.findall('review:(.+?)\.\.\.', str)
TA贡献1770条经验 获得超3个赞
使用
re.findall(r'\breview:\s*(.*?)\s*\.\.\.', string)
import re
regex = r"\breview:\s*(.*?)\s*\.\.\."
string = "review: I love you very much... reviewer:jackson review: I hate you very much... reviewer:madden review: sky is pink and i ... reviewer: tom"
print ( re.findall(regex, string) )
输出:['I love you very much', 'I hate you very much', 'sky is pink and i']
请注意,r"..."表示原始字符串文字的前缀"\b"不是单词边界,而是r"\b"。
解释
NODE EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
review: 'review:'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount possible))
--------------------------------------------------------------------------------
\.\.\. '...'
--------------------------------------------------------------------------------
TA贡献2039条经验 获得超7个赞
您可以使用以下利用前瞻的模式:
(?<=review:\s).*?(?=\.\.\.)
inp = "review: I love you very much... reviewer:jackson review: I hate you very much... reviewer:madden review: sky is pink and i ... reviewer: tom"
matches = re.findall(r'(?<=review:\s).*?(?=\.\.\.)', inp)
print(matches)
TA贡献1890条经验 获得超9个赞
re.findall与模式一起使用\breview:\s*(.*?)\.\.\.\s*(?=\breviewer:|$):
inp = "review: I love you very much... reviewer:jackson review: I hate you very much... reviewer:madden review: sky is pink and i ... reviewer: tom"
matches = re.findall(r'\breview:\s*(.*?)\.\.\.\s*(?=\breviewer:|$)', inp)
print(matches)
这打印:
['I love you very much', 'I hate you very much', 'sky is pink and i ']
添加回答
举报