Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Write Regular Expression to Match Either A or B in Python



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']

Updated on: 2023-11-02T13:22:22+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements