Commonly used regular expression operators
Re library
Regular expressions (English names: regular expression, regex, RE) are expressions used to concisely express a set of character strings. The main application is in string matching.
Mode introduction:
1).re.I(re.IGNORECASE): Ignore case
2).re.M(MULTILINE): Multi-line mode, change the behavior of'^' and'$'
3).re.S(DOTALL): Click any matching mode to change the behavior of'.'
4).re.L(LOCALE): Make the predetermined character class \w \W \b \B \s \S depend on the current locale setting
5).re.U(UNICODE): Make the predetermined character class \w \W \b \B \s \S \d \D depend on the character attribute defined by unicode
6).re.X (VERBOSE): detailed mode. In this mode, the regular expression can be multiple lines, ignore whitespace characters, and can add comments
Function introduction:
1、re.search(pattern,string,flags=0)
Search for the first position of a matching regular expression in a string, and return a match object.
pattern: string or native string representation of regular expression
string: the string to be matched
flags: control flags when using regular expressions, which mode
The first:
# -*- coding = utf-8 -*-
import re
# 创建模式对象
pat = re.compile("aa") # 此处的aa是正则表达式
reh = pat.search("aaa") # search后的字符串是被校验的字符串
print(reh)
# <re.Match object; span=(0, 2), match='aa'>
The second type:
pat = re.search("aa","aaa")
print(pat)
# <re.Match object; span=(0, 2), match='aa'>
2、re.match(pattern,string,flags=0)
Match the regular expression from the beginning of a string and return the match object.
pattern: string or native string representation of regular expression
string: the string to be matched
flags: control flags when using regular expressions
The first:
pat = re.match("a","aaaa")
print(pat)
# <re.Match object; span=(0, 1), match='a'>
The second type:
pat = re.compile("a")
p = pat.match("aaaa")
print(p)
# <re.Match object; span=(0, 1), match='a'>
3、re.findall(pattern,string,flags=0)
Search string, return all matching substrings in list type.
pattern: string or native string representation of regular expression
string: the string to be matched
flags: control flags when using regular expressions
The first:
pat = re.findall("a","aassaaddd");
print(pat)
# ['a', 'a', 'a', 'a']
The second type:
# 创建模式对象
pat = re.compile("a") # 此处的aa是正则表达式
re = pat.findall("aassaaddd") # search后的字符串是被校验的字符串
print(re)
4、re.split(pattern,string,maxsplit=0,flags=0)
Search string, return all matching substrings in list type.
pattern: string or native string representation of regular expression
string: the string to be matched
maxsplit: the maximum number of splits, the remaining part is output as the last element
flags: control flags when using regular expressions
The first:
pat = re.split("a","hadhks",2)
print(pat)
# ['h', 'dhks']
5、re.finditer(pattern,string,flags=0)
The search string returns an iteration type of the matching result, and each iteration element is a match object.
pattern: string or native string representation of regular expression
string: the string to be matched
flags: control flags when using regular expressions
The first:
pat = re.finditer("a","aabbss")
for i in pat:
print(i)
# <re.Match object; span=(0, 1), match='a'>
# <re.Match object; span=(1, 2), match='a'>
6、re.sub(pattern,repl,string,count=0,flags=0)
Replace all substrings matching the regular expression in a string, and return the replaced string.
pattern: string or native string representation of regular expression
repl: replace the string matching the string
string: the string to be matched
count: the maximum number of matching replacements
flags: control flags when using regular expressions
The first:
pat = re.sub("a","b","aaaadddvvv",5)
print(pat)
# bbbbdddvvv
Another equivalent usage of the Re library:
Functional usage: one-time operation
rst = re.search(r'[1-9]\d{5}', 'BIT 100081')
Object-oriented usage: multiple operations after compilation
pat = re.compile(r'[1-9]\d{5}')
rst = pat.search('BIT 100081')
regex = re.comile(pattern,flags=0)
Compile the string form of the regular expression into a regular expression object
pattern: string or native string representation of regular expression
flags: control flags when using regular expressions
Regex is the regular expression: regex = re.compile(r'[1-9]\d{5}')
Add r before the matched string to avoid escape characters
s = r"'ssssss\'"
print(s)
# 'ssssss\'