0

I have the following piece of code:

s="sometext AB89NS0137JY more text AB1234"
pattern='AB[0-9A-Z].+'
print(re.findall(pattern,s))

which yields

['AB89NS0137JY more text AB1234']

I assumed this should return ['AB89NS0137JY','AB1234']. I am probably missing something really elementary here, but I've been breaking my head over this. Could somebody explain how that piece of code should be changed to return ['AB89NS0137JY','AB1234']?

geo909
  • 296
  • 1
  • 4
  • 15
  • 3
    Remove the `.` from `.+` as it will repeat any char except a newline 1 or more times. – The fourth bird Apr 14 '20 at 09:02
  • 3
    See https://ideone.com/sUMwSm, `.` matches any char (with the exception of line break chars by default). You should have quantified the character class, not add a `.` before the quantifier. `.` is not part of a quantifier. – Wiktor Stribiżew Apr 14 '20 at 09:04
  • 1
    Thanks a lot.. I'd be happy to accept any of these as an answer – geo909 Apr 14 '20 at 09:09

0 Answers0