Regular expressions (the basics)

The basic regular expression rules:
. 1, \ D is a matching numerical example 0-9 interval: str = '1232 ten million'
2 + Matches the previous character is repeated one or more times
3, * matching character zero appears in front of or multiple
3.1, '+' and ' ' distinction:
Example: 'ab & +': the expression is: ab & comprises two elements must appear once every ah, the 'ab &
' indicates that: ab & obtaining a earliest position, b the number of occurrence times may be, for example, 0; a first position two or more times can be crawled appeared ab

4 ,? Repeat the previous character zero obtain a matching one for example: '? Ab' abbca regular =, where b is repeated only once
5 ,. represent any one character, but does not mean the character '\ n' when not otherwise stated
6, | on behalf of the left and right divided into two parts: 'satisfy one of which can be a'
7, \ for example: '\ r', '\ n', '\ t', '\', each represents a carriage return. Line feed, tabulation symbols backslash itself itself.
. 8, \ B indicates the end of a word, a word ending character or a blank including various end of the string.
9, [] is arbitrarily selected in a character, if the character code is SAcll continuous set, you can use the '-' a symbolic link,
for example, [0-9] 0-9 wherein is represented by a number.
10, appears in the ^ [^ 0-9] indicates, among other letters and numbers 0-9
11, \ s matches any whitespace, equivalents "[\ F \ T \ V \ R & lt \ on the x20]"
12 is, \ w underscore character matching words including, equivalent to [a-zA-Z0-0-9_]
13 is, beginning with AD ^ ad 13.2 $ ad ending in AD
14, parentheses (...) ... is seen as a a whole; often with +, * ,? , Continuous use of (...) is repeated portion.

Examples
Search:
#search () function: the entire string looks for pattern matching, only the first to find a match and return an object matching information, the subject method can be obtained by calling the string matching Group (), If the string does not match, None is returned
findall

Return all the results match the rule, on the list:

Examples #:
Import Re
str1 = '1232 ten million' objects #
reg = '\ d +' # regular
result = re.findall (reg, str1) ==> [ '1232']

Examples # 2:
Import Re
str1 = 'ABCABC' objects #
reg = 'ab +' # regular
result = re.search (reg, str1)
results; ==> <_sre.SRE_Match object; span = (2, 4), match = 'ab'>

reg = ‘ab*’
结果:==> <_sre.SRE_Match object; span=(0, 1), match=‘a’>

Example 5:

Re Import
str1 = 'akbcabc' objects #
reg = 'ak | ab # regular
result = re.search (reg, str1) # is wherein K.
==> <_sre.SRE_Match Object; span = (0,. 3), match = 'akb'>

Example 6:

Re Import
str1 = 'akbcabc' objects #
reg = 'ab # regular
result = re.search (reg, str1) # is wherein K.
==> <_sre.SRE_Match Object; span = (0, 2), match =' ak '>

Examples. 7:
Import Re
str1 = 'A \ nkbcabc' objects #
reg = 'a \ nk # regular
result = re.search (reg, str1) # where ==> <_sre.SRE_Match object; span = (0, 4) , match = 'AK'>
Print (result.group ())
==> A
K

Examples #. 8:
Import Re
str1 = 'Hello Wrold' objects #
reg = r'hello \ b '# regular ==' Hello \ D '
Result = the re.search (REG, str1) where # ==> <_sre.SRE_Match Object; span = (0,. 4), match = 'AK'>
Print (result.group ())

Examples #. 9
Import Re
str1 = 'Hello 123 Wrold' objects #
reg = '[az] +' # regular
result = re.findall (reg, str1) # wherein
Print (Result)
== "[ 'Hello', 'Wrold ']

Examples # 10
Import Re
str1 = 'Hello 123 Wrold' objects #
reg = '[^ 123] + ' # regular
result = re.findall (reg, str1) # wherein
Print (Result)
== "[ 'Hello', ' wrold ']

Examples #. 11
Import Re
str1 = 'Hello 123 Wrold' objects #
reg = '\ s +' # regular
result = re.findall (reg, str1) # wherein
Print (Result)
[ '', '']

Examples # 12 is
Import Re
str1 = 'hello_123_wrold' objects #
reg = '\ w [^ _ ] *' # regular
result = re.findall (reg, str1) # wherein
Print (Result)
[ 'Hello', '_123', '_wrold']

发布了4 篇原创文章 · 获赞 0 · 访问量 23

Guess you like

Origin blog.csdn.net/weixin_44824389/article/details/104319281