Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The.findall () function

example

import re
a = '123abc'
re=re.findall('\d\d\d',a)
print(re)
Copy the code

Import the RE library used to complete regular expression operations

Extract the text using the findAll () function in the RE library

'\d' means match 1 digit, so '\d\d\d' means match 3 consecutive digits

Syntax for the

Re.findall (Matching rules, original document)Copy the code

Note: The findAll () function returns a list of extracted results

import re
a = '456qwe789rty123abc'
re=re.findall('\d\d\d',a)
print(re)
print(re[0])
Copy the code

symbol meaning
\d Matches 1 numeric character
\w Matches 1 letter, number, or underscore character
\s Matches 1 whitespace character (newline, TAB, plain space, etc.)
\S Matches 1 non-whitespace character
\n Match 1 newline character “In” (equivalent to pressing Enter once)
\t Match 1 Tab character “\t” (equivalent to pressing Tab once or space eight times)
. Matches 1 character except newline
* Matches zero or more expressions
+ Matches one or more expressions
? With “.” Used in conjunction with * to form a non-greedy match
(a) Matches the expression in parentheses, also representing a group

For example,

import re
a = '456qwe789rty123abc'
re=re.findall('\d\d\d\w',a)
print(re)
Copy the code

import re
a = '456qwe789rty123abc'
re=re.findall('\d\d\d\w.',a)
print(re)
Copy the code