Matching any one of several patterns

If there is a fixed number of patterns that would constitute a match, they can be combined using the following syntax:

(<pattern1>|<pattern2>|<pattern3>)

The following a_or_b regular expression will match any string where there is either an a character or a b character:

....
a_or_b = re.compile("(a|b)")
if a_or_b.search("a"):
print("'a' is a match")
if a_or_b.search("b"):
print("'b' is a match")
if a_or_b.search("c"):
print("'c' is a match")
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset