A pipeline can match one of several regular expressions
>>>
>>> M=re.search (R ' batman| Tina Fey ', ' Batman and Tina Fey ')
>>> print (M.group ())
Batman
>>> M=re.search (R ' batman| Tina Fey ', ' Tina Fey and Batman ')
>>> print (M.group ())
Tina Fey
>>>
The question mark indicates that the character or grouping in front of it is optional in this mode, matching "0 or one"
>>> batregex= Re.compile (R ' Bat (wo)? man ')
>>> M1=batregex.search (' I Am a Batman ')
>> > Print (M1.group ())
Batman
>>> m2= Batregex.search (' I am a Batwoman ')
>>> print (M2.group ())
batwoman
> >>
In the above regular expression, Wo is optional, can match Batman, but also can match Batwoman
>>>
>>> Phoneregex=re.compile (R ' (\d{3}-)? \d{3}-\d{4} ')
>>> m1=phoneregex.search (' My phone number is 021-456-2345 ')
>>> Print (M1.group ())
021-456-2345
>>> m2=phoneregex.search (' My phone number is 456-2345 ')
>>> Print (M2.group ())
456-2345
>>>
The phone number that matches the regular expression above can be either with an area code or without an area code
An asterisk means "match 0 or more times"
>>>
>>> batregex=re.compile (R ' Bat (wo) *man ')
>>> m=batregex.search (' I am a Batman ')
>>> Print (M.group ())
Batman
>>> m=batregex.search (' I am a Batwoman ')
>>> Print (M.group ())
Batwoman
>>> m=batregex.search (' I am a Batwowoman ')
>>> Print (M.group ())
Batwowoman
>>>
A plus sign means "match one or more times"
>>>
>>> batregex=re.compile (R ' Bat (wo) +man ')
>>> m=batregex.search (' I am a Batwoman ')
>>> Print (M.group ())
Batwoman
>>> m=batregex.search (' I am a Batwowoman ')
>>> Print (M.group ())
Batwowoman
>>> m=batregex.search (' I am a Batman ')
>>> m = = None
True
>>>
Curly braces match a specific number of times
(HA) {2} matches ' haha '
(HA) {2,} matches 2 times or more ha
(HA) {. 2} matches 0 to 2 ha
(HA) {2.5} matches 2 to 5 ha
Python Regular Expression match times