python regular expression re. findall usage

  • 2020-12-26 05:47:18
  • OfStack

Python regular expressions

A regular expression is a special sequence of characters that makes it easy to check if a string matches a pattern.

Python has added the re module since version 1.5, which provides Perl-style regular expression patterns.

The re module enables the Python language to have full regular expression functionality.
The compile function generates a regular expression object based on a pattern string and optional flag parameters. This object has a family of methods for regular expression matching and substitution.

The re module also provides functions that function exactly 1 with these methods, using a 1 pattern string as their first argument.

This article mainly introduces the usage of regular expression re.findall in python, as shown below.

In python, by integrating the re module inline, a program lady can call directly to achieve regular matching.

Where the re. findall() function can iterate over the matches, get all the matching strings in the string, and return a list.

In the python source code, this is shown as follows: Search for string and return 1 iterator that accesses every 1 match sequentially (Match object). Find all the substrings that RE matches and return them as an iterator.


def findall(pattern, string, flags=0):

The first parameter, the regular expression

The second argument, which searches for those strings

The third argument, matches the pattern, where re.S makes all characters match, including line breaks. The findall() function matches line by line.

Returns all strings in string that match pattern in the form of an array

If you want to use the ES57en.findall function, you must refer to the rs package


import re
import re
regular_v1 = re.findall(r"docs","https://docs.python.org/3/whatsnew/3.6.html")
print (regular_v1)

The result: ['docs'] is an array

Returns a string ending in html with the $symbol to determine whether the string ends in a string


import re
regular_v3 = re.findall(r"html$","https://docs.python.org/3/whatsnew/3.6.html")
print (regular_v3)

The result: ['html'] is an array

conclusion


Related articles: