Python matching has many precautions when we use it. We will encounter many problems during continuous learning. Next we will take a detailed look at how to better master the related Python matching technical issues. Regular Expression object version
- Rereobj = re. compile (r "\ Z") # End with \ Z at the end of the regular expression
- If reobj. match (subject ):
- Do_something ()
- Else:
- Do_anotherthing ()
Create a regular expression object and obtain Python matching details through this object.
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- # match start: match.start()
- # match end (exclusive): match.end()
- # matched text: match.group()
- do_something()
- else:
- do_anotherthing()
Obtain a Python matched substring using a regular expression object
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group()
- else:
- result = ""
Obtain the Python-matched substring of the capture group using the regular expression object
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group(1)
- else:
- result = ""
Use a regular expression object to obtain the Python-matched substring of a famous group
- rereobj = re.compile(regex)
- match = reobj.search(subject)
- if match:
- result = match.group("groupname")
- else:
- result = ""
Use a regular expression object to obtain all Python matched substrings and put them in an array
- rereobj = re.compile(regex)
- result = reobj.findall(subject)
Traverse all Python matched substrings by using a regular expression object
- rereobj = re.compile(regex)
- for match in reobj.finditer(subject):
- # match start: match.start()
- # match end (exclusive): match.end()
- # matched text: match.group
The above is a detailed introduction to Python matching.