2

I have the following code:

testREString = '(hello | goodbye) \s darkness \s my \s old \s friend'
testString = 'sound of silence: goodbye darkness my old friend'
exp = re.compile(testREString, re.VERBOSE)
print(exp.findall(testString))

and it returns: ['goodbye'] where I had expected it to return the full sentence - in fact, using exp.search(testString) it had picked out the rest of the sentence correctly. Why then, was the complete match not displayed?

Thank you for your time.

CowNorris
  • 421
  • 2
  • 10
  • 2
    Because `(...)` is a capturing group, and [`findall`](https://docs.python.org/2/library/re.html#re.findall) returns the content of the capturing groups if you defined any. Make it a non-capturing group `(?:...)` – khelwood Jul 21 '17 at 15:16
  • To make it non-capturing, just add `?:` at the beginning of the parentheses. [docs](https://docs.python.org/2/library/re.html) – patrick Jul 21 '17 at 15:18

2 Answers2

3

(...) in a regular expression defines a capturing group.

re.findall returns the content of the capturing groups if your expression defines any.

You can make it a non-capturing group (?:hello|goodbye) to avoid this. See What is a non-capturing group?

khelwood
  • 46,621
  • 12
  • 59
  • 83
2

khelwood has explained why findall() behaves this way. If you want to catch the whole match without changing the regex, use

exp.group(0)
knipknap
  • 5,182
  • 5
  • 34
  • 40