亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 python 正則表達式中獲取兩個子字符串中的特定字符串?

如何在 python 正則表達式中獲取兩個子字符串中的特定字符串?

海綿寶寶撒 2023-03-16 17:44:56
這是示例:review: I love you very much... reviewer:jackson review: I hate you very much... reviewer:madden review: sky is pink and i ... reviewer: tom我想提取字符串review:和之間的字符串...所以以上情況的提取是I love you very muchI hate you very muchsky is pink and i 我使用這種正則表達式但失敗了re.findall("review(.*)...",string)它提取了這種結果:I love you very much... reviewer:jackson review: I hate you very much... reviewer:madden review: sky is pink and i
查看完整描述

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)


查看完整回答
反對 回復 2023-03-16
?
德瑪西亞99

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

--------------------------------------------------------------------------------

  \.\.\.                   '...'

--------------------------------------------------------------------------------


查看完整回答
反對 回復 2023-03-16
?
largeQ

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)


查看完整回答
反對 回復 2023-03-16
?
當年話下

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


查看完整回答
反對 回復 2023-03-16
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

\對不起,我忘了在前面 添加.

正確的是: re.findall("review:\b?(.*)\.\.\.",string)

而這一次,很重要


查看完整回答
反對 回復 2023-03-16
  • 5 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號