0.
1. Reference
Python Regular Expression Guide
Https://docs.python.org/2/library/re.html
Https://docs.python.org/3/library/re.html
String |
Re |
Note |
|
Re.match (Pattern, string, flags=0) |
At the start of the string |
S.find (Sub [, Start [, end]]) int |
Re.search (Pattern, string, flags=0) |
Scan through string looking for a match |
S.replace (old, new[, Count]), string |
Re.findall (Pattern, string, flags=0) |
Re.finditer |
2. Group M.group ()
Xx
in [560]: M.group? Docstring:group ([Group1, ...])-Strortuple. Return subgroup (s) of the match by indicesornames. For 0 returns the entire match. Type:builtin_function_or_methodin [542]: M=re.search (r'(-{1,2} (GR))','Pro---gram-files') in [543]: M.group ()#comes withOUT[543]:'--gr'In [544]:m.group (0) #comes with, returns the entire match to the string for 0 returns the entire match. Note that m.string is the complete original text that was retrieved ... OUT[544]:'--gr'In [545]: M.group (1) out[545]:'--gr'In [546]: M.group (2) out[546]:'GR'In [547]: M.group (3)#added ( not satisfied with the error ---------------------------------------------------------------------------Indexerror Traceback (most recent)<ipython-input-547-71a2c7935517>inch<module>()----> 1 M.group (3) Indexerror:no such groupin [548]: M.group#Select multiple groupings to return a tupleOUT[548]: ('--gr','GR') in [549]: M.groups()#Select all GroupsOUT[549]: ('--gr','GR')
M.groupdict for named groupings
in [557]: M.groupdict? Docstring:groupdict ([Default=none])Dict. Return A dictionary containing all the named subgroups of the match,keyed by the subgroup name. The default argument isUsed forGroupsthat did notParticipateinchThe Matchtype:builtin_function_or_methodin [558]: M=re.search (r'(-{1,2} (? P<GR>GR))','Pro---gram-files') in [559]: m.groupdict () out[559]: {'GR':'GR'}
3. Extract Re.findall ()
Re.findall (Pattern, string, flags=0)
in [+]: Text ="He was carefully disguised and captured quickly by police."In [98]: Re.findall (r"\w+ly", text) #相当于 m.group (0) out[98]: ['carefully','quickly']in [[In]: Re.findall (r"(\w+) ly", text) #手动加单个括号限定内容, equivalent to returning M.group (1) out[99]: ['careful','Quick']in [+]: Re.findall (r"( (\w+) (ly))", text) #多个括号, from left to right (the equivalent of returning m.groups () out[100]: [('carefully','careful','ly'), ('quickly','Quick','ly')]
In [102]: Re.findall (R "((1\w+) (ly))", text)
OUT[102]: []
4. Replace Re.sub ()
Re.sub (Pattern, Repl, String, count=0, flags=0)
The forward reference in Repl backreferences, such as, is replaced with the \6
substring matched by group 6 in the pattern. You can also use the Func Realize.
In [158]:deffunc (M): ...:returnM.group ('DEF')+' '+m.group (2)#aliases...: in [159]: Re.sub (r'(? p<def>DEF) \s+ ([a-z]+) \s*\ (\s*\):',func,'def func (): Def f ():') out[159]:'def func def F'In []: Re.sub (r'(? P<DEF>DEF) \s+ ([a-z]+) \s*\ (\s*\):',R ' \1 \2 ','def func (): Def f ():')#do not support \ aliasesOUT[160]:'def func def F'
5. Backreferences forward reference in pattern5.1 poker for a pair
In [204]: Re.search (R'(.). *\1','ab123') in [205]: Re.search (R'(.). *\1','ab121') out[205]: <_sre. Sre_match at 0x71ca120> in[206]: _.group () out['121'
5.2 Consecutive multiple identical
In [207]: Re.search (r'. {3}','1122')#ErrorOUT[207]: <_sre. Sre_match at 0x71b94a8>In [208]: Re.search (r'(.) {3}','1122')#ErrorOUT[208]: <_sre. Sre_match at 0x71ca198>In [209]: Re.search (r'(.) \1\1','1122')#correctIn ["Re.search" (r'(.) \1\1','1112') out[[]: <_sre. Sre_match at 0x71ca210>In [211]: Re.search (r'(.) \1{2}','1112') out[211]: <_sre. Sre_match at 0x71ca288>In [212]: _.group () out[212]:'111'
Python's re is simple enough