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條經驗 獲得超8個贊
您可以使用以下利用前瞻的模式:
(?<=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)
這打?。?/p>
['I love you very much', 'I hate you very much', 'sky is pink and i ']
添加回答
舉報