1

Here is the example:

review: I love you very much... reviewer:jackson review: I hate you very much... reviewer:madden review: sky is pink and i ... reviewer: tom

i want extract the string between string review: and ...

So above situation's extraction is

I love you very much

I hate you very much

sky is pink and i

i use this kind of regex but fail

re.findall("review(.*)...",string)

it extract this kind of outcome:

I love you very much... reviewer:jackson review: I hate you very much... reviewer:madden review: sky is pink and i

5 Answers 5

2

this will also work and it's simple

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)
Sign up to request clarification or add additional context in comments.

Comments

1

Use re.findall with the pattern \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)

This prints:

['I love you very much', 'I hate you very much', 'sky is pink and i ']

Comments

1

You can use following pattern which utilizes lookaheads:

(?<=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)

Comments

1

Use

re.findall(r'\breview:\s*(.*?)\s*\.\.\.', string)

See proof. Python test:

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

Output: ['I love you very much', 'I hate you very much', 'sky is pink and i']

Note the r"..." prefix signalling raw string literal since "\b" is not a word boundary, and r"\b" is.

EXPLANATION

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))
--------------------------------------------------------------------------------
  \.\.\.                   '...'
--------------------------------------------------------------------------------

Comments

-1

Sorry i forget to add \ in front of .

and the right one is: re.findall("review:\b?(.*)\.\.\.",string)

and this time , it counts

3 Comments

This will not work. You want a non-greedy match. Please have a look at ? in the capture in my answer.
Also, please look at my answer :-)
Good, great help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.