-1

I want to match sth like 12.12a or 13.12b but the below regex match with 'a' and i have no clue why is like that

import re

pattern = re.compile('\d\d?\.\d\d?(a|b)')

txt = "12.12a"

pattern_list = re.findall(pattern,txt)

for item in pattern_list:
    print(item) # result a
Felix Ha
  • 401
  • 5
  • 11

1 Answers1

0

Put the expression into the brackets. When there are brackets, only staff in brackets (matching groups to be precize) are matched and returned

pattern = re.compile('(\d\d?\.\d\d?(a|b))')

The result is ('12.12b', 'a') because of internal brackets. To get rid of internal brackets matches, use item[0] or another appropriate operation. Or simply unfold the regex (might be a little bit slower)

pattern = re.compile('\d\d?\.\d\d?a|\d\d?\.\d\d?b')
Serge
  • 3,387
  • 3
  • 16
  • 34