语法
/pattern/修饰符
我的2种理解:
- 在targetStr中找符合patternStr的串
- 判断targetStr是否符合patternStr的规则
Pattern
字符类
例子 | ||
---|---|---|
[] | [字符序列],字符类(字符集) | [Tt]he "The"或"the" => The car parked in the garage. ar[.] "ar." => A garage is a good place to park a car. |
[^字符序列],否定的字符类(反向类) | [^c]ar c以外的一个字符 + "ar" => The car parked in the garage. [ ^cp]ar c、p以外的一个字符 + "ar" => The car parked in the garage. |
|
[字符-字符…], 范围类 | [a-z] ,小写字母 [a-zA-Z] ,字母 [a-z-] ,小写字母还有“-” |
预定义字符类
对应字符类 | 含义 | 例子 | |
---|---|---|---|
. | [^
] |
回车和换行以外的字符 | ".ar" => The car parked in the garage. |
ucode | code对应的unicode字符 | ||
d | [0-9] |
数字字符 | |
s | [
x0Bf] |
空白符 | |
w | [a-zA-Z_0-9] |
单词字符(数字、字母、_) | |
D | [^0-9] |
非数字字符 | |
S | [^
x0Bf] |
非空白符 | |
W | [^a-zA-Z_0-9] |
非单词字符 |
汉字字符
[u4E00-u9FA5]
表示任意汉字
pattern里可以直接用中文字符的
边界
含义 | |
---|---|
单词边界 | |
B | 非单词边界 |
^ | 开始(写在字符前),从行首匹配 |
$ | 结束(写在字符后),从行末匹配 |
量词
含义 | 例子 | |
---|---|---|
? | 0次或1次 | [T]?he => The car is parked in the garage. he和The |
+ | 一次或多次(至少一次) | c.+t => The fat cat sat on the mat. 第一个c,最后一个t以及之间的字符串(之间字符串位空则不匹配) |
* | 任意次(可以是0次) | [a]* 所有的a => The fat cat sat on the concatenation. [a-z]* 所有的小写字母 => The fat cat sat on the concatenation. |
{n} | 出现n次 | |
{n,m} | n到m次 | |
{n,} | 至少n次 | |
{0,m} | 至多m次 |
贪婪匹配
贪婪匹配:次数取区间最大值
非贪婪匹配:量词后加?
,次数取区间最小值
分组()
# 分组前面有字符时,前面有.*才能匹配到
re.match('.*([u4E00-u9FA5]{2})大学', '在南京大学学习')
re.match('([u4E00-u9FA5]{2})大学', '在南京大学学习')
或|
pattern1|pattern2
满足pattern1或者pattern2
修饰符
含义 | |
---|---|
g |
全局匹配,默认只得到第一次匹配结果 |
i |
忽略大小写,默认不忽略大小写 |
m |
多行匹配,默认只匹配一行(因为换行符在系统看来就是 ) |
不同语言
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public static void main(String[] args){
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher_obj = pattern.matcher(targetStr);
if(matcher_obj.matches()){
// matcher_obj.group(1) 匹配内容的第1个分组(分组从1开始)
System.out.println("group1=" + matcher_obj.group(1));
}
}
patternString里需要转义,比如
d
要用\d
Python
import re
# re.match(patternStr, targetStr) 进行正则匹配
match_obj = re.match(patternStr, targetStr)
# match_obj 没有匹配到时为空
if matchobj:
# match_obj.string 匹配的所有内容
print(match_obj.group(0))
# match_obj.groups(1) 匹配内容的第1个分组(分组从1开始)
print(match_obj.group(1))
# match_obj.groups() 匹配内容的分组(数组格式), 没有分组时为空
print(match_obj.groups())
patternString里不用转义,比如
d
就用d
而不是\d