Import re
Help (Re.compile)
‘‘‘
The output is:
Help on function compile in module re:
Compile (pattern, flags=0)
Compile a regular expression pattern, returning a pattern object.
Through Help: Compile a regular expression pattern and return a schema object.
‘‘‘
‘‘‘
The second parameter, flags, is a matching pattern that can be used by a bitwise or ' | ' Indicates that it takes effect at the same time, or it can be specified in a regular expression string.
The pattern object cannot be instantiated directly, but only through the compile method. The matching modes are:
1). Re. I (re. IGNORECASE): Ignore case
2). Re. M (MULTILINE): Multiline mode, changing the behavior of ' ^ ' and ' $ '
3). Re. S (dotall): Point any matching pattern, change '. ' The behavior
4). Re. L (LOCALE): Make predefined character class \w \w\b\b \s \s depends on the current locale setting
5). Re. U (UNICODE): Make predefined character classes \w \w\b\b \s \s \d \d depends on character attributes defined by Unicode
6). Re. X (VERBOSE): Verbose mode. In this mode, the regular expression can be multiple lines, ignore whitespace characters, and can add comments
‘‘‘
text="Jgod is a handsome boy, but he's a ider"
Print Re.findall (R ' \w*o\w* ', text)#查找有o的单词
#输出结果为: [' jgod ', ' Handsome ', ' boy ']
#利用compile生成一个规则模式吧, and then use FindAll to match an object's content. , appropriate to output the content that conforms to the rule
Regex=re.compile (R ' \w*o\w* ')
Print Regex.findall (text)
#>>> [' Jgod ', ' Handsome ', ' boy ']
Test1= "Who do you are,what do do,when you getget there?" What is time there? "
Regex1=re.compile (R ' \w*wh\w* ', re. IGNORECASE)
Wh=regex1.findall (test1)
Print WH
#>>> ["Who", "what", "when", ' What ')
‘‘‘
The RE regular expression module also includes some useful functions for manipulating regular expressions. The following mainly describes the match function and the search function.
Definition: Re.match attempts to match a pattern from the beginning of a string.
Prototype:
Re.match (pattern, string, flags)
The first parameter is a regular expression, and if the match succeeds, a match is returned, otherwise none is returned;
The second parameter represents the string to match;
The third parameter is the Peugeot bit, which controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
The return value of the function is true or FALSE.
For example: Match (' P ', ' Python ') return value is true; Match (' P ', ' www.python.org ') return value is false.
Definition: Re.search looks for the first substring in a given string that matches a given regular expression.
The return value of the function: Returns the value if found, otherwise returns none.
Prototype:
Re.search (pattern, string, flags)
Each parameter has the same meaning as Re.match.
‘‘‘
#re. Match Example 1
Import re
Your_love=re.match ("WH","What is you doing?" Who's you mate? ", Re. I)
If Your_love:
Print"You Are my angle"
Else
Print"I Lose You"
#相当于:
Print"*"*#便于区分
Import re
Content= "What is youdoing?" Who's your mate? "
Regu_cont=re.compile ("\w*wh\w*", re. I)
Yl=regu_cont.match (content)
If YL:
print yl.group (0)
Else
print "what happen?"
Parsing: First created a string content that requires regular expression matching;
Then we use Re.compile () to create the matching rules we need, and create the schema object Regu_cont;
YL used to receive the result of the match function implemented by Regu_cont regular expressions on the content contents string
If a yl is not empty, the substring found by using the M.group (index) output is otherwise (the return value is None) print "What happen?"
Match Example 2
‘‘‘
Match if the results are found, a matchobject is returned, and you can query matchobject about the matching string. Matchobject instances also have several methods and properties, the most important of which are as follows:
Group () returns a string that is matched by RE
Start () returns the position where the match started
End () returns the position of the end of the match
Span () returns a tuple containing the position of the match (start, end)
‘‘‘
Import re
Content="What is you doing?" Who's your mate? "
Regu_cont=re.compile ("\w*wh\w*",re. I)
Yl=regu_cont.match (content)
if yl:
print yl.group (0)
else:
print "pass the test"
print Yl.group ()
print Yl.start ()
print yl.end ()
print Yl.span ()
Executes the result:
What
what
0
4
(0 4)
#search () method is similar to the match () method
import Recontent= ' Where is your from? So hansome. ' Regex=re.compile (r ' \w*som\w* ') m=regex.search (content) if m: print m.group (0) else: print "not found"
#相当于:
import Re
M=re.search ( r ' \w*som\w* ' , "Where is From? So handsome. ' ,re. I)
if m:
Print M.group (0)
else:
print " not found "
Python's re-module understanding (Re.compile, Re.match, Re.search)