#1 character Matching
Import re
R=r ' AB '
Print Re.findall (R, ' ABCDEFGABCD ')
#2 If you have metacharacters, be aware that you use the escape character ' \ '
R=r ' 1\*2 '
Print Re.findall (R, ' 01*234501*2345 ')
#元字符介绍
#3 ^ Match beginning of Line
R=r ' ^123 '
Print Re.findall (R, ' 1234512345 ')
#4 $ match Line end
R=r ' 345$ '
Print Re.findall (R, ' 1234512345 ')
#5. Match single character
R=r ' 3.5 '
Print Re.findall (R, ' 1234512345 ')
#6 [] matches a specified character set
R=r ' A[a-za-z0-9]c '
Print Re.findall (R, ' Abc,acc,a5c,a3c,cfg ')
#7 [^] complement matches characters that are not in range, note ^ put in front
R=r ' A[^0-9]c '
Print Re.findall (R, ' Abc,acc,a5c,a3c,cfg ')
#特殊意义字符
#8 \d representative number [0-9]
R=r ' A\DC '
Print Re.findall (R, ' Abc,acc,a5c,a3c,cfg ')
#9 \d Non-digital
R=r ' A\DC '
Print Re.findall (R, ' Abc,acc,a5c,a3c,cfg ')
#10 \s whitespace characters
R=r ' A\SC '
Print Re.findall (R, ' Abc,acc,a5c,a3c,cfg,a c,a C ')
#11 \s non-whitespace characters
R=r ' A\SC '
Print Re.findall (R, ' Abc,acc,a5c,a3c,cfg,a c,a C ')
#12 \w word character [a-za-z0-9_]
R=r ' A\WC '
Print Re.findall (R, ' Abc,acc,a5c,a3c,cfg,a c,a c,a_c ')
#13 \w non-word characters
R=r ' A\WC '
Print Re.findall (R, ' Abc,acc,a5c,a3c,cfg,a c,a c,a_c ')
#14 {} The number of repetitions of the previous character
R=r ' A{2}c '
Print Re.findall (R, ' 1AAC,2AACC,3AAAAAC ')
#15 the number of repetitions of the previous character within a range
R=r ' A{1,4}c '
Print Re.findall (R, ' AC,AACC,AAAAAC ')
#16 * Match the previous character by more than or equal to 0 times, {0,} effect equivalent to *
R=r ' 1[a]*c '
Print Re.findall (R, ' 1c2,1aacc2,1aaaaac2 ')
#17 + matches the previous character more than or equal to 1 times, {1,} effect equivalent to +
R=r ' 1[a]+c '
Print Re.findall (R, ' 1c2,1aacc2,1aaaaac2 ')
#18? Match the previous character 0 or 1 times to indicate whether the {0,1} effect is equivalent to?
R=r ' 1[ab]?c '
Print Re.findall (R, ' 1C,1AC2,1BC2,1ABC2 ')
#19 *? Match as few as possible
R=r ' 1.*?c '
Print Re.findall (R, ' 1abc1,1abc1,c ')
#20 () grouping, note that the ' \.com\.cn ' is placed in front, the whole plus parentheses as a large grouping to easily see which matches to which
R=r ' (\[email protected]\w+ (\.com\.cn|\.com|\.cn)) '
Print Re.findall (r, ' [email protected] ')
The results of the operation are as follows:
>>>
1 [' AB ', ' AB ']
2 [' 1*2 ', ' 1*2 ']
3 [' 123 ']
4 [' 345 ']
5 [' 345 ', ' 345 ']
6 [' abc ', ' ACc ', ' a5c ', ' a3c ']
7 [' abc ', ' ACc ']
8 [' a5c ', ' a3c ']
9 [' abc ', ' ACc ']
Ten [' A C ']
One [' abc ', ' ACc ', ' a5c ', ' a3c ']
[' abc ', ' ACc ', ' a5c ', ' a3c ', ' A_c ']
[' A C ']
[' AAC ', ' AAC ', ' AAC ']
[' AC ', ' aac ', ' AAAAC ']
[' 1c ', ' 1AAC ', ' 1AAAAAC ']
[' 1aac ', ' 1AAAAAC ']
[' 1c ', ' 1ac ', ' 1BC ']
[' 1abc ', ' 1,1abc ', ' 1,c ']
[(' [email protected] ', '. com.cn ')]
>>>
This article from "Today's efforts, tomorrow's success!" "Blog, be sure to keep this provenance http://zhzhgo.blog.51cto.com/10497096/1675498
Python Regular Expression Basics