Article From:

In recent week, I have learned some basic contents of regular expressions with online classes, and put them on my blog to deepen my impression.

I. The Concept of First Form Regular Expressions

  A single string is used to describe matching a series of strings that conform to a certain syntactic rule; a logical formula for string manipulation; to process text and data; to compare expressions with characters in text at one time; if each character matches, the matching will succeed, otherwise the matching will fail.

  pythonThe re library can be used to implement regular expressions, using re. compile () to generate a regular expression object pattern, and using pattern. match () method or pattern. search () method to get matching results.

  Specific methods:

  ①search(pattern,string,flags=0):Find matches in a string

     str1=’hello2018′’\d+’,str1) The match is 2018.

  ②findall(pattern,string,flags=0):Find a match and return a list of all matched parts


      info=re.findall(r’\d’,str2) You can match [‘100′,’90’,’80’] if you use search (), you can only match to 100.

  ③sub(pattern,repl,string,count=0,flags=0):Replace matching regular expressions in strings with other values

      def add1(match):


          return str(num)

      str3=’this year is 2018′

       re.sub(r’\d+’,add1,str3) Pass the matched 2018 to function ADD1 and get 2019 after execution.

  ④split(pattern,string,maxsplit=0,flags=0):Returns a list of partitioned strings based on matching


      info=re.split(r’,|\||.|,’,str4)Use specific symbols as separators for Strings

  ⑤match(pattern,string,flags=0):Matching strings from scratch

      info=re.match(r’\d+’,2018) Match from the beginning

2. Basic Contents of Regular Expressions

1、Matching a single character

  . Match arbitrary characters (except\n)

  […]Matching Character Set

  \d / \D Matched Number/Non-Number

  \s / \S Matching blank/non-blank characters

  \w / \W Matching Word Characters [a-zA-Z0-9]/Non-Word Characters

  Example: Ma = re. match (r’.’,’a’):’. ‘can match any character, so it can match’a’, using ma. group () to get a

2、Matching multiple characters

  * Match the previous character 0 or infinite times

  + Match the previous character once or infinitely

  ? Match the previous character 0 or 1 times

  {m} / {m,n} Match the previous character m times or m to N times

  *? / +? / ?? Matching mode becomes non-greedy (matching characters as few as possible)

  Example: Match a python variable name (consisting of a number of underscores, beginning with letters and underscores)


3、Boundary matching

  ^ Match the beginning of the string

  $ Match the end of the string

  \A / \Z The specified string must appear at the beginning/end (\A after the character\Z before the character)

  Example: (r’A [w]* Z’,’_098′) matches strings with” at the beginning and end

4、Packet matching

  | Match any left or right expression

(ab)Expressions in parentheses act as a grouping

  \<number>A string matched by a reference to a grouping num

  (P<name>) Grouping up an individual name

  (?P=name) Group Matching String Referring to Alias Name

  Examples: Ma = re. match (r'[ w] {4,6} @ (163 | 126). com’,’’)

             ma=re.match(r'<[\w]+>[\w]+</\1′,'<book>python</book>’) (This \ represents the previous & lt; & gt; matched book)

             ma=re.match(r'<(?P<mark>[\w]+>)[\w]+</(?P=mark)>’,'<book>python</book>’) ((?P=mark)Represents the previously matched book.


Link of this Article: Basis of Regular Expressions

Leave a Reply

Your email address will not be published. Required fields are marked *