h33
h33

Reputation: 1344

RegEx - Or operator

I am executing this.

print(re.findall(r'(<OR|<PP).*>', '<OR first><PP second><OR third>'))

Expected:

['<OR first>', '<PP second>', '<OR third>']

Actual:

['<OR']

Does any body know how I can achieve the expected?

Upvotes: 0

Views: 714

Answers (1)

Sebastian Proske
Sebastian Proske

Reputation: 8413

import re
print(re.findall(r'(?:<OR|<PP)[^>]*>', '<OR first><PP second><OR third>'))

Note that

  • findall only returns captured groups if there are any, otherwise the full match
  • .* matches greedily, so your pattern matches the whole string

Upvotes: 3

Related Questions