0

I have a paragraph of text like this:

John went out for a walk. He met Mrs. Edwards and said, 'Hello Mam how are you doing today?'. She replied 'I'm fine. How are you?'.

I would like to capture the words within the single quotes. I tried this regex

re.findall(r"(?<=([']\b))((?=(\\?))\2.)*?(?=\1))",string)

(from this question: RegEx: Grabbing values between quotation marks)

It returned only single quotes as the output. I don't know what went wrong can someone help me?

a stone arachnid
  • 1,272
  • 1
  • 15
  • 27

1 Answers1

2

Python requires capturing groups to be fully closed before any backreferences (\2) to the group.

You can use Positive Lookbehind (?<=[\s,.]) and Positive Lookahead (?=[\s,.]) zero-length assertions to match words inside single quotes, including words such as I'm, i.e.:

re.findall(r"(?<=[\s,.])'.*?'(?=[\s,.])", string)

Full match  56-92   'Hello Mam how are you doing today?'
Full match  106-130 'I'm fine. How are you?'

Explanation enter image description here


Regex Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268