hutool.core.util.regex¶
正则表达式工具类,对应 Java cn.hutool.core.util.ReUtil
Module Contents¶
Classes¶
正则表达式工具类,对应 Java cn.hutool.core.util.ReUtil |
API¶
- class hutool.core.util.regex.ReUtil¶
正则表达式工具类,对应 Java cn.hutool.core.util.ReUtil
提供常用的正则匹配、提取、替换、分割等静态方法。
- static is_match(pattern: str, content: str) bool¶
给定内容是否匹配正则(全匹配)
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
是否全匹配
- static contains(pattern: str, content: str) bool¶
给定内容是否包含正则匹配
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
是否包含匹配
- static get(pattern: str, content: str, group_index: int = 0) Optional[str]¶
获得匹配的字符串,group_index=0 返回整个匹配
- 参数:
pattern – 正则表达式
content – 被匹配的内容
group_index – 匹配组索引,0表示整个匹配
- 返回:
匹配的字符串,无匹配返回 None
- static get_all(pattern: str, content: str, group_index: int = 0) List[str]¶
获得所有匹配的字符串列表
- 参数:
pattern – 正则表达式
content – 被匹配的内容
group_index – 匹配组索引,0表示整个匹配
- 返回:
所有匹配的字符串列表
- static get_all_groups(pattern: str, content: str) List[List[str]]¶
获得所有匹配,每个匹配返回所有分组
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
二维列表,外层为每个匹配,内层为该匹配的所有捕获组(group(1) 至 group(N))
- static match_all(pattern: str, content: str) List[str]¶
获得匹配的全部内容(等价于 get_all group_index=0)
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
所有完整匹配的字符串列表
- static replace_all(content: str, pattern: str, replacement: str) str¶
替换所有匹配
- 参数:
content – 被替换的内容
pattern – 正则表达式
replacement – 替换字符串,可使用 1 2 等反向引用
- 返回:
替换后的字符串
- static replace_first(content: str, pattern: str, replacement: str) str¶
替换第一个匹配
- 参数:
content – 被替换的内容
pattern – 正则表达式
replacement – 替换字符串,可使用 1 2 等反向引用
- 返回:
替换后的字符串
- static extract(pattern: str, content: str, template: str) Optional[str]¶
提取匹配内容并按模板重组
例如:
extract(r"(\d+)-(\d+)", "123-456", "$2-$1") -> "456-123" extract(r"(\w+)@(\w+)", "user@host", "$1") -> "user"
- 参数:
pattern – 正则表达式,应包含分组
content – 被匹配的内容
template – 模板字符串,使用 $1 $2 等引用分组
- 返回:
按模板重组后的字符串,无匹配返回 None
- static extract_multi(pattern: str, content: str, template: str) List[str]¶
提取所有匹配内容并按模板重组
- 参数:
pattern – 正则表达式,应包含分组
content – 被匹配的内容
template – 模板字符串,使用 $1 $2 等引用分组
- 返回:
所有按模板重组后的字符串列表
- static count(pattern: str, content: str) int¶
统计匹配次数
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
匹配次数
- static split(content: str, pattern: str) List[str]¶
按正则分割字符串
- 参数:
content – 被分割的字符串
pattern – 分隔符正则表达式
- 返回:
分割后的字符串列表
- static del_all(pattern: str, content: str) str¶
删除所有匹配内容
- 参数:
pattern – 正则表达式
content – 被处理的字符串
- 返回:
删除匹配内容后的字符串
- static escape(characters: str) str¶
转义正则特殊字符
- 参数:
characters – 需要转义的字符串
- 返回:
转义后的字符串
- static get_group0(pattern: str, content: str) Optional[str]¶
获取第一个匹配的第 0 组(整个匹配)
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
整个匹配的字符串,无匹配返回 None
- static get_group1(pattern: str, content: str) Optional[str]¶
获取第一个匹配的第 1 组
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
第 1 组匹配的字符串,无匹配返回 None
- static _apply_template(match: re.Match, template: str) str¶
将匹配对象按模板重组
模板中 $n(n 为数字)会被替换为对应的捕获组内容。 $$ 会被替换为字面量 $。
- 参数:
match – 正则匹配对象
template – 模板字符串
- 返回:
重组后的字符串
- static find_all(pattern: str, content: str, group_index: int = 0) List[str]¶
查找所有匹配(与 get_all 相同)。
- 参数:
pattern – 正则表达式
content – 被匹配的内容
group_index – 匹配组索引
- 返回:
所有匹配的字符串列表
- static find_all_group0(pattern: str, content: str) List[str]¶
查找所有 group(0) 匹配。
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
所有完整匹配的列表
- static find_all_group1(pattern: str, content: str) List[str]¶
查找所有 group(1) 匹配。
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
所有 group(1) 匹配的列表
- static find_first_number(content: str) Optional[str]¶
查找第一个数字。
- 参数:
content – 被匹配的内容
- 返回:
第一个数字字符串,无匹配返回 None
- static index_of(pattern: str, content: str) int¶
第一个匹配的起始位置。
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
起始位置,无匹配返回 -1
- static last_index_of(pattern: str, content: str) int¶
最后一个匹配的起始位置。
- 参数:
pattern – 正则表达式
content – 被匹配的内容
- 返回:
最后匹配的起始位置,无匹配返回 -1
- static del_first(pattern: str, content: str) str¶
删除第一个匹配。
- 参数:
pattern – 正则表达式
content – 被处理的字符串
- 返回:
删除第一个匹配后的字符串
- static del_last(pattern: str, content: str) str¶
删除最后一个匹配。
- 参数:
pattern – 正则表达式
content – 被处理的字符串
- 返回:
删除最后一个匹配后的字符串
- static del_pre(pattern: str, content: str) Optional[str]¶
删除匹配及其之前的内容。
- 参数:
pattern – 正则表达式
content – 被处理的字符串
- 返回:
删除匹配前内容后的字符串,无匹配返回 None
- static extract_multi_and_del_pre(pattern: str, content: str, group_index: int = 0) List[str]¶
提取所有匹配并删除匹配前的内容(原地消费)。
依次匹配,每次匹配后从内容中删除该匹配及其之前的部分, 返回所有匹配结果。
- 参数:
pattern – 正则表达式
content – 被处理的字符串
group_index – 匹配组索引
- 返回:
所有匹配的列表
- static get_all_group_names(pattern: str) Dict[str, int]¶
获取正则表达式中所有命名捕获组。
- 参数:
pattern – 正则表达式
- 返回:
命名组名到组索引的映射字典
- static replace_by_func(content: str, pattern: str, func: Callable[[re.Match], str]) str¶
正则替换,使用回调函数处理每个匹配。
- 参数:
content – 被替换的内容
pattern – 正则表达式
func – 回调函数,接受 re.Match 对象,返回替换字符串
- 返回:
替换后的字符串
- static find_first_pattern(content: str, patterns: List[str]) Optional[str]¶
按优先级(列表顺序)查找第一个匹配的正则模式。
- 参数:
content – 待检查的字符串
patterns – 正则表达式列表
- 返回:
第一个匹配的模式字符串,无匹配返回
None
- static find_all_patterns(content: str, patterns: List[str]) List[str]¶
查找所有匹配的正则模式。
- 参数:
content – 待检查的字符串
patterns – 正则表达式列表
- 返回:
所有匹配的模式列表