diff --git a/doc/readme.md b/doc/readme.md index 71b991d..de9ea46 100644 --- a/doc/readme.md +++ b/doc/readme.md @@ -871,6 +871,25 @@ jsonpath的规则参见[jsonpath-rust](https://github.com/besok/jsonpath-rust) 返回替换结果 + +### 正则替换 + + +【正则替换@文本@正则表达式@新文本】 + +返回替换结果 + +例如: +``` +【正则替换 +@(met)1875159423(met) +@\\(met\\)(?P(\\d+)|(all))\\(met\\) +@[CQ:at,qq=$qq] +】 +``` +将返回`[CQ:at,qq=1875159423]` + + ### 文本查找 diff --git a/res/version.txt b/res/version.txt index b0fea7d..9990aec 100644 --- a/res/version.txt +++ b/res/version.txt @@ -1 +1 @@ -0.0.65 \ No newline at end of file +0.0.66 \ No newline at end of file diff --git a/src/redlang/exfun.rs b/src/redlang/exfun.rs index b7c11d3..4d04019 100644 --- a/src/redlang/exfun.rs +++ b/src/redlang/exfun.rs @@ -2369,6 +2369,14 @@ def red_out(sw): } Ok(Some("".to_string())) }); + add_fun(vec!["正则替换"],|self_t,params|{ + let text = self_t.get_param(params, 0)?; + let re = self_t.get_param(params, 1)?; + let out_text = self_t.get_param(params, 2)?; + let re_obj = fancy_regex::Regex::new(&re)?; + let out = re_obj.replace_all(&text, out_text).to_string(); + Ok(Some(out)) + }); }