Regular Expression module re: Regular Expression common characters, common optional symbols, group and groups, match, search, sub, findall, compile, and findallcompile

Source: Internet
Author: User

Regular Expression module re: Regular Expression common characters, common optional symbols, group and groups, match, search, sub, findall, compile, and findallcompile
Re:

  • Introduction: Regular Expression Module
  • Regular Expression characters:
Match * Character before 0 Once or multiple times
  Meaning Example [# subsequent results]
. . Match any character, except \ n
^ ^ The string to be followed must start with the string to be matched; otherwise, the string cannot be found.

$

$ The preceding string must end with the string to be matched; otherwise, the string cannot be found.
\ D Match a number.
\D Match non-Numbers
\ S It can match a blank character (space, indentation, \ n, \ r)
     
You can use + ,? , * To select the matching times    
+ It indicates matching the previous character once or multiple times, greedy
 
? It indicates that the first character is matched 0 or 1 time, not greedy
* Match*Character before0Greedy,
     
Available[]Range Note: The value range is determined by yourself and is represented by-. For example, [0-9], [1-9], [1-6], and so on.  
[A-z] Indicates that the matching range is a-z.
[0-9] Indicates that the matching range is 0-9.
You can also set multiple ranges:
A-zA-Z0-9
Match letters or numbers
     
Or: | Used in two centers,Match | left or | right character,For exampleA | B representativeCan match A or B
     
{M} can be used to indicate the number of matching times.    
{N} Match the first character n times
{N ,} Match n or more times before a character
{N, m} RepresentativeMatch the first character n to m times
     
Available(...) To indicate group matching Indicates matching a whole block in (), which can be used for a group of data.
     
     
     

More: https://baike.baidu.com/item/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/1700215? Fr = aladdin

Common optional flag:

Re. S: Make. Match All characters including line breaks

Re. I: case-insensitive matching

If you want to use multiple flag spaces at the same time, you need to use |:

 

Group and groups:

The returned results of matching match and search are all objects. To obtain the corresponding string, use group (num) or groups ():

Group (num = 0 ):

If you call it directly, the entire matching result is returned,

If the group contains a parameter: group (0) indicates the entire matching result, group (1) lists the matching part of the first group, and group (2) lists the matching part of the second group, group (3) lists the matching parts of the third group, and so on.


Groups ()

Returns the matched characters of all groups as tuples.

 
 
Additional:
  •  start([group])The method is used to obtain the starting position (index of the first character) of the substring in the entire string. The default value of the parameter is 0;
  • end([group])The method is used to obtain the end position of the substring in the entire string (index + 1 of the last character of the substring). The default value is 0;
  • span([group])Method return(start(group), end(group)).
 
Re. match (pattern, string, flags = 0 ):

Function:Re. match: match from the beginning. If the start of the string does not match, None is returned]

Parameter introduction:

  • Pattern: matched Regular Expression
  • String: the string to be matched.
  • Flags: A flag used to control the matching mode of regular expressions, such as case-sensitive or multi-row matching.

Re. search (pattern, string, flags = 0 ):
Function:Re. search to search the entire string and return the first matching result.
Parameter introduction:

Pattern: matched Regular Expression

String: the string to be matched.

Flags: flags used to control the matching mode of regular expressions.

Re. sub (pattern, repl, string, count = 0, flags = 0 ):
Function:Re. sub is used to replace the matching items in the string. You can specify the number of replicas.

Parameter introduction:

Pattern: The pattern string in the regular expression.

Repl: a replacement string. It can also be a function.

String: the original string to be searched and replaced.

Count: the maximum number of times a pattern is replaced. The default value 0 indicates that all matches are replaced.

Usage:

import reprint(re.sub("abc","ABC","123abc123"))#123ABC123print(re.sub("abc","ABC","123abc123abc123abc",2))#123ABC123ABC123abcprint(re.sub("abc","ABC","123abc123abc123abc",2))#123ABC123ABC123abcdef func(x):    x=int(x.group())+1    return str(x)print(re.sub("123",lambda x:str(int(x.group())+1),"123abc123"))#124abc124print(re.sub("123",func,"123abc123"))#124abc124

 

 

 

Re. findall (string [, pos [, endpos]):
Function:Re. findall searches for the entire string and returns all matched strings with elements in the list.

Parameter introduction:

  • String: the string to be matched.
  • Pos: an optional parameter that specifies the start position of a string. The default value is 0.
  • Endpos: an optional parameter that specifies the end position of a string. The default value is the string length.

Usage:

 
Re. compile Function
  • The compile function is used to compile regular expressions and generate a regular expression (Pattern) object.
  • Note: it can only be used by the match () and search () functions.

Import reprint (". ". center (50, '-') print (re. match (". "," abc ") # <_ sre. SRE_Match object; span = (0, 1), match = 'A'> print (re. match (". "," abc "). group () # aprint (re. match (". "," abc "). groups () # aprint ("+ ". center (50, '-') print (re. match ("a +", "aaaa "). group () # aaaaprint ("? ". Center (50, '-') print (re. match ("? "," Aaaa "). group () # aprint ("*". center (50, '-') print (re. match ("a *", "aaaa "). group () # aaaaprint ("^ ". center (50, '-') print (re. search ("^. B "," acbd "). group () # acbprint (re. match ("^. + "," abc "). group () print (re. search ("^. B "," 123 acbd ") # print (re. search (". + d $ "," acbd "). group () # acbdprint (re. search (". + d $ "," acbdc ") # print ("". center (50, '-') print ("\ d ". center (50, '-') print (re. match ("\ d", "123456 "). group () #1 print (re. match ("\ d +", "123456 "). group () #123456 print ("\ D ". center (50, '-') print (re. search ("\ D", "123456b "). group () # bprint (re. search ("\ D", "a123456 "). group () # aprint ("\ s ". center (50, '-') print (re. search ("a \ sb", "123a b456 "). group () # a bprint ("[]". center (50, '-') print (re. search ("[a-z] +", "abcdefg "). group () # abcdefuplint (re. search ("[a-k] +", "abczefg "). group () # abcprint (re. search ("[0-9] +", "123456 "). group () #123456 print (re. search ("[0-4] +", "123456 "). group () #1234 print (re. search ("[a-zA-Z0-9] +", "1a2bC456ef "). group () # 1a2bC456efprint ("". center (50, '-') print (re. search ("[a-z] + | [A-Z] +", "1ab2bC4ef "). group () # abprint (re. search ("([a-z] | [A-Z]) +", "1ab2bC4ef "). group () # abprint ("{n} {n, m }". center (50, '-') print (re. search ("[a-z] {3}", "1ab2bC4efg "). group () # efuplint (re. search ("[a-z] {2, 3}", "1ab2bC4efg "). group () # abprint (re. search ("[a-z] {2, 3}", "1a2C4efg "). group () # efuplint (re. search ("[a-z] {2,}", "1a2C4efgaaaa "). group () # efgaaaaprint ("group matching ". center (50, '-') print (re. search ("([a-z] | [A-Z]) +", "1ab2bC4ef "). group () # abprint (re. search ("([a-z] | [A-Z]) +", "1ab2bC4ef "). group () # abprint ("group groups ". center (50, '-') print (re. search ("(\ d [a-z] \ d) {3}", "1x11a32a465 "). group () # 1x11a32a4print (re. search ("(abc) {3}", "abcabcabc123 "). group () # abcabcabcprint (re. search ("(abc)", "abcabcabc123 "). groups () # ('abc',) m = re. search ("(abc) (CBA) (def)", "abccbadef123") print (m. groups () # ('abc', 'ba', 'def ') print (m. group (0) # abccbadefprint (m. group (1) # abcprint (m. group (2) # cbaprint ("findall ". center (50, '-') print (re. findall ("(abc)", "abcabcabc123") # ['abc', 'abc', 'abc'] print ("flag ". center (50, '-') print (re. search (". B "," a \ nb ", re. S ). group () # a bprint (re. search (". B "," A \ nb ", re. S | re. I ). group () # A bprint (re. search ("AB", "AB", re. I ). group () # AB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.