Matching rules for a single character (and regular expressions re module)

What is a regular expression:

Personal understanding thereof: according to certain rules, matching the desired data from a string.

The standard explanation: regular expressions, also known as regular expressions. (English: Regular Expression, the code is often abbreviated as regex, regexp or RE), a concept in computer science. Regular expressions are typically used to retrieve, replace the text in line with those of a model (rule) is. -------Baidu Encyclopedia

Regular expressions commonly used matching rules:

Matches a string:

text = "hello"
retext = re.match("he",text)
print(retext.group())

That he can match the text.

1. can only follow the match, that is, if the beginning is not he, will complain.

2. .group type () to be matched can be read into the text.


(.) Matches any character points:

text = "+hello"
ret = re.match(".",text)
print(ret.group())

+ Matched.

 

\ D: numeric characters match any of (0-9)

text = "0+35"
ret = re.match('\d',text)
print(ret.group())

 

\ D: matches any non-numeric characters

text = "+"
ret = re.match('\D',text)
print(ret.group())

 

\ S: Match whitespace character (\ t, \ r, \ n)

text = " \nab "
ret = re.match('\s',text)
print(ret.group())

 

\ W: match az, AZ, numbers and underscore

text = 'a'
ret = re.match('\w',text)
print(ret.group())

 

\ W: to a match \ w complementary

text = '++a'
ret = re.match('\W',text)
print(ret.group())

 

[] In combination: As long as satisfying the characters in brackets, can be matched

text = '0731-8888888'
ret = re.match('[\d\-]',text)
print(ret.group())
Wherein [\ D \ -] indicates a match \ D (0-9) matches or -. The second \ represents the escape.

  With [] above indicates a match:
1) instead of using brackets \ d:
text = '+09asd+-sad/+'
ret = re.match('[0-9]',text)
print(ret.group())
2) instead of using brackets \ D:
text = '+09asd+-sad/+'
ret = re.match('[^0-9]',text)
print(ret.group())
. 3) with brackets instead of \ w:
text = 'A+09asd+-sad/+'
ret = re.match('[a-zA-Z0-9_]',text)
print(ret.group())
. 4) instead of using brackets \ W:
text = '+09asd+-sad/+'
ret = re.match('[^a-zA-Z0-9_]',text)
print(ret.group())
Wherein ^ represents the negated sign.

Guess you like

Origin www.cnblogs.com/zyde-2893/p/11184037.html