猿问

如何在 python 正则表达式中获取两个子字符串中的特定字符串?

这是示例:

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 much

I hate you very much

sky 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


海绵宝宝撒
浏览 129回答 5
5回答

潇潇雨雨

这也可以,而且很简单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)

德玛西亚99

使用re.findall(r'\breview:\s*(.*?)\s*\.\.\.', string)见证明。蟒蛇测试:import reregex = 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))--------------------------------------------------------------------------------  \.\.\.                   '...'--------------------------------------------------------------------------------

largeQ

您可以使用以下利用前瞻的模式:(?<=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)

当年话下

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

慕虎7371278

\对不起,我忘了在前面&nbsp;添加.正确的是:&nbsp;re.findall("review:\b?(.*)\.\.\.",string)而这一次,很重要
随时随地看视频慕课网APP

相关分类

Python
我要回答