Skip to content

Commit

Permalink
新增命令【正则替换】
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Oct 3, 2023
1 parent f594621 commit e2cbc83
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
19 changes: 19 additions & 0 deletions doc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,25 @@ jsonpath的规则参见[jsonpath-rust](https://github.com/besok/jsonpath-rust)

返回替换结果


### 正则替换


【正则替换@<font color="red">文本</font>@<font color="red">正则表达式</font>@<font color="red">新文本</font>】

返回替换结果

例如:
```
【正则替换
@(met)1875159423(met)
@\\(met\\)(?P<qq>(\\d+)|(all))\\(met\\)
@[CQ:at,qq=$qq]
```
将返回`[CQ:at,qq=1875159423]`


### 文本查找


Expand Down
2 changes: 1 addition & 1 deletion res/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.65
0.0.66
8 changes: 8 additions & 0 deletions src/redlang/exfun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
});
}


Expand Down

0 comments on commit e2cbc83

Please sign in to comment.