0

I am new to Python and Regex. I was checking some tutorial regarding Regex and as per my understanding, all the codes below should have given me same output but I am not. Can someone please explain why I am not getting it.

In this example I am trying to find Batman or Batwoman in the paragraph.

import re

BatRegex = re.compile(r'Batman|Batwoman')
mo = BatRegex.findall('The adventures of Batman and Batwoman')
print(mo)
['Batman', 'Batwoman']

BatRegex = re.compile(r'Bat(man|woman)')
mo = BatRegex.findall('The adventures of Batman and Batwoman')
print(mo)
['man', 'woman']

BatRegex = re.compile(r'Bat(wo)?man')
mo = BatRegex.findall('The adventures of Batman and Batwoman')
print(mo)
['', 'wo']
Prince Modi
  • 425
  • 1
  • 4
  • 16
  • 1
    See [`findall` reference](https://docs.python.org/2/library/re.html#re.findall): *If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.* All details and work-arounds are [here](https://stackoverflow.com/a/31915134/3832970). – Wiktor Stribiżew Mar 13 '20 at 22:07
  • 2
    Consider using non-capture groups with `?:` : `BatRegex = re.compile(r'Bat(?:man|woman)')` and `re.compile(r'Bat(?:wo)?man')` – Mark Mar 13 '20 at 22:08
  • It worked. Is this a regex thing or a python 3 thing? – Prince Modi Mar 13 '20 at 22:10
  • 2
    @PrinceModi it's a regex thing. Here's a thread that's better than the marked dupe: https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-in-regular-expressions – Mark Mar 13 '20 at 22:12

0 Answers0