How to use the compile function of Python
Today, I would like to share with you the relevant knowledge points about how to use the compile function of Python. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Example
> import re
> pattern = re.compile (r'([a murz] +) ([a murz] +)', re.I) # re.I means ignore case
> m = pattern.match ('Hello World Wide Web')
> print (m) # matches successfully and returns a Match object > m.group (0) # returns the entire substring 'Hello World'
> m.span (0) # returns the index of the entire substring that matches successfully (0,11)
> m.group (1) # returns the first packet matching substring 'Hello'
> m.span (1) # returns the index (0,5) of the first packet matching successful substring
> m.group (2) # returns the second packet matching substring 'World'
> m.span (2) # returns the substring index of the second packet with a successful match (6,11)
> m.groups () # is equivalent to (m.group (1), m.group (2),...) ('Hello',' World')
> m.group (3) # there is no third grouping
Traceback (most recent call last):
File "", line 1, in
IndexError: no such group
These are all the contents of the article "how to use the compile function of Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.