>>> pet = re.compile(r'dog') >>> type(pet) <class'_sre.SRE_Pattern'> >>> bool(pet.search('They bought a dog')) True >>> bool(pet.search('A cat crossed their path')) False
函数
函数
说明
re.findall
返回包含所有匹配项的列表
re.finditer
返回一个可迭代的匹配对象 (每个匹配一个)
re.search
如果字符串中的任何位置存在匹配项,则返回 Match 对象
re.split
返回一个列表,其中字符串在每次匹配时被拆分
re.sub
用字符串替换一个或多个匹配项
re.compile
编译正则表达式模式供以后使用
re.escape
返回所有非字母数字反斜杠的字符串
Flags 标志
:-
-
-
re.I
re.IGNORECASE
忽略大小写
re.M
re.MULTILINE
多行
re.L
re.LOCALE
使 \w、\b、\slocale 依赖
re.S
re.DOTALL
点匹配所有 (包括换行符)
re.U
re.UNICODE
使 \w、\b、\d、\sunicode 依赖
re.X
re.VERBOSE
可读风格
JavaScript 中的正则表达式
RegExp
属性
:-
:-
dotAll
是否使用了 s 修饰符
flags
返回标志的字符串
global
是否使用了 g (全部)修饰符
hasIndices
是否使用了 d 修饰符
ignoreCase
匹配文本的时候是否忽略大小写 i
multiline
是否进行多行搜索 m
lastIndex
该索引表示从哪里开始下一个匹配
source
正则表达式的文本
sticky
搜索是否是 sticky
unicode
Unicode 功能是否开启
方法
:-
:-
match()
获取匹配结果
matchAll()
所有匹配项
replace()
替换所有符合正则模式的匹配项
search()
搜索以取得匹配正则模式的项
split()
切割字符串返回字符串数组
compile()
(重新)编译正则表达式
exec()
指定字符串中执行一个搜索匹配
test()
正则表达式与指定的字符串是否匹配
toString()
返回该正则表达式的字符串
test()
1 2 3 4 5 6
let textA = 'I like APPles very much'; let textB = 'I like APPles'; let regex = /apples$/i console.log(regex.test(textA)); // false console.log(regex.test(textB)); // true
search()
1 2 3 4 5 6
let text = 'I like APPles very much'; let regexA = /apples/; let regexB = /apples/i;
let text = 'Do you like apples?'; let regex= /apples/; // Output: apples console.log(regex.exec(text)[0]); // Output: Do you like apples? console.log(regex.exec(text).input);
match()
1 2 3 4 5
let text = 'Here are apples and apPleS'; let regex = /apples/gi; // Output: [ "apples", "apPleS" ] console.log(text.match(regex));
split()
1 2 3 4 5
let text = 'This 593 string will be brok294en at places where d1gits are.'; let regex = /\d+/g // Output: [ "This ", " string will be brok", "en at places where d", "gits are." ] console.log(text.split(regex))
matchAll()
1 2 3 4 5 6 7
let regex = /t(e)(st(\d?))/g; let text = 'test1test2'; let array = [...text.matchAll(regex)]; // Output: ["test1", "e", "st1", "1"] console.log(array[0]); // Output: ["test2", "e", "st2", "2"] console.log(array[1]);
replace()
1 2 3 4 5 6
let text = 'Do you like aPPles?'; let regex = /apples/i // Output: Do you like mangoes? let result = text.replace(regex, 'mangoes'); console.log(result);
Stringregex="[A-Z\n]{5}$"; Stringstr="I like APP\nLE"; Patternp= Pattern.compile(regex, Pattern.MULTILINE); Matcherm= p.matcher(str); // Outputs: I like Apple! System.out.println(m.replaceAll("pple!"));
所有匹配的数组:
1 2 3 4 5 6 7 8 9 10
Stringstr="She sells seashells by the Seashore"; Stringregex="\\w*se\\w*"; Patternp= Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcherm= p.matcher(str); List<String> matches = newArrayList<>(); while (m.find()) { matches.add(m.group()); } // Outputs: [sells, seashells, Seashore] System.out.println(matches);
MySQL中的正则表达式
函数
函数名称
说明
REGEXP
字符串是否匹配正则表达式
REGEXP_INSTR()
匹配正则表达式的子字符串的起始索引 (注意:仅限 MySQL 8.0+)
REGEXP_LIKE()
字符串是否匹配正则表达式 (注意:仅 MySQL 8.0+)
REGEXP_REPLACE()
替换匹配正则表达式的子字符串 (注意:仅限 MySQL 8.0+)
REGEXP_SUBSTR()
返回匹配正则表达式的子字符串 (注意:仅 MySQL 8.0+)
REGEXP
1
expr REGEXP pat
Examples
1 2 3 4 5 6 7
mysql>SELECT'abc' REGEXP '^[a-d]'; 1 mysql>SELECT name FROM cities WHERE name REGEXP '^A'; mysql>SELECT name FROM cities WHERE name NOT REGEXP '^A'; mysql>SELECT name FROM cities WHERE name REGEXP 'A|B|R'; mysql>SELECT'a' REGEXP 'A', 'a' REGEXP BINARY'A'; 10
REGEXP_REPLACE
1
REGEXP_REPLACE(expr, pat, repl[, pos[, occurrence[, match_type]]])
例子
1 2 3 4
mysql>SELECT REGEXP_REPLACE('a b c', 'b', 'X'); a X c mysql>SELECT REGEXP_REPLACE('abc ghi', '[a-z]+', 'X', 1, 2); abc X