Atomic
An atom is the most basic constituent unit of a regular expression, with at least one atom in each regular expression. Common types of atoms are:
A ordinary character as an atom
b nonprinting characters as atoms
C Universal characters as atoms
D Atomic table.
ImportRe String="Taoyunjiaoyu"#ordinary characters as atomspat="Yum"rst=Re.search (pat,string)Print(RST)#non-printable characters as atoms#\ n line break \ t tabstring='"" TaoyunjiaoyuBaidu'pat="\ n"rst=Re.search (pat,string)Print(RST)#universal characters as atoms\w Letters, numbers, underscores \w except for letters, numbers, underscores \d decimal digits \d except for decimal digits \s white space characters \s in addition to white space characters string=" "taoyunji8 7362387aoyubaidu ' "pat=" \w\d\s\d\d "Rst=re.search (pat,string) print (rst) #原子表string = ' ' Taoyunji87362387aoyubaidu "pat=" tao[abd] "pat=" Tao[^abd] "Rst=re.search (pat,string) print (RST)
View Code
Metacharacters
The so-called meta-character, is the regular expression has some special meaning of the characters, such as repeating n times before the characters and so on.
. Any one character except newline ^ start position $ end Position * 0\1\ multiple times? 01 Times + 1\ Multiple {n} exactly n times {n,} at least n times in,m} at least N, up to M times | pattern selector or () mode unit
View Code
Pattern Repair Symbol
The so-called pattern modifier, which can change the meaning of the regular expression by the pattern modifier without changing the regular expression, realizes some functions such as adjusting the matching result.
I ignore case when matching
string="Python"Pat="pyt"rst= Re.search (Pat,string,re. I)print(RST)
View Code
M multi-line matching
L Localization Identification match
U Unicode
s let. Match include line break
Greedy Mode & Lazy mode
The core point of greedy mode is to match as many as possible, while the core of lazy mode is to match as few as possible.
# greedy mode with lazy mode String= " povthonyhjskjsa " PAT1 = p.*y " # greedy mode Pat2= " p.*?y " # lazy mode rst= =re.search (PAT2,STRING,RE.L) print print (RST2)
View Code
Regular-expression functions
Re.match () function: Match from the beginning, matching a
Re.search () function: matches from any position, matching a
Global matching function
Global match format: Re.compile (regular expression). FindAll (data)
Regular expressions in Python