Experience using python regular expressions

  • 2020-04-02 09:20:38
  • OfStack

1. Match () matches from the starting position
2. Search () matches any position, if there are more than one matches, only the first is returned
3. Finditer () returns all matches
4. For every match, try to match as much as possible. Such as:
> > > M = re.com running (' ABC (BCD) * b)
> > > M. indall (' abcbcbcb)
[' abcbcbcb]
Abcbcb is also a match for ABC [BCD]*b, but only returns a maximum match.
5. The split () method
A. The maximum number of partitions can be specified based on the regular partition of a string
> > > P = re.com running (r '\ W +')
> > > P/s ('This is a test, short and sweet, of split().')
[' This' and 'is',' a ', 'test', 'short', 'and', 'sweet' and 'of', 'split', ']
B. Sometimes, you are not only interested in the text between the delimiters, you also need to know what the delimiter is.
If the capture brackets are used in RE, their values are also returned as part of the list. Compare the following calls:
> > > P2 = re.com running (r '(\ W +))
[' This', '... ', 'is',', 'a', ', 'test', '. ', ']


Related articles: