How to write a regular expression to match either a or b in Python?

PythonServer Side ProgrammingProgramming

The following code uses a regular expression '(a|b)' to match a or b in the given Python string

We are also using the flag re.I to ignore case of a or b while matching

Example

import re
s = 'Bank of Baroda'
print(re.findall(r'(a|b)',s, re.I))

Output

This gives the output

['B', 'a', 'B', 'a', 'a']

raja
Published on 08-Jan-2018 13:01:48
Advertisements