#regexp
Xu 0.16.0 API
导入方式: #regexp
公开 API: 2 函数, 14 方法, 4 类型, 0 常量, 0 方法集
匹配、查找、分割、展开和替换使用共享工作预算及显式堆栈。调用时须处理错误,例如 matched := re.match_string(&text) >>> (err) { << 1 }。预算耗尽返回错误,正常未匹配仍返回空值或 false。表达式最多 65,536 字节、64 层分组。quote_meta、num_subexp 与 literal_prefix 不执行匹配。
struct Regexp
pub struct Regexp {
pub nodes []Node
pub root int
pub class_data string
pub steps_max int
pub num_caps int
}
用途
承载 nodes、root、class_data、steps_max、num_caps 等属性的结构体。
公开字段
nodes []Node- 字段([]Node)
root int- 字段(int)
class_data string- 字段(string)
steps_max int- 字段(int)
num_caps int- 字段(int)
quote_meta
pub fn quote_meta(s &string) string
用途
执行 quote meta 操作。
参数
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
返回值
string — 结果字符串
compile
pub fn compile(expr string) Regexp
用途
执行 compile 处理。失败时抛出错误,调用方需处理或继续传播。
参数
expr string- expr(string)
返回值
Regexp — 该类型的值
(Regexp) num_subexp
pub fn (re Regexp) num_subexp() int
用途
Regexp 上的操作:执行 num subexp 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
返回值
int — 该类型的值
(Regexp) find_submatch
pub fn (re Regexp) find_submatch(s &string) []string
用途
Regexp 上的操作:执行 find submatch 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
返回值
[]string — 切片结果
struct Submatch
pub struct Submatch {
pub start int
pub end int
pub parts []string
}
用途
承载 start、end、parts 等属性的结构体。
公开字段
start int- 字段(int)
end int- 字段(int)
parts []string- 各段
(Regexp) find_all_submatch
pub fn (re Regexp) find_all_submatch(s &string, n int) [][]string
用途
Regexp 上的操作:执行 find all submatch 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
n int- 数量、长度或上限(int)
返回值
[][]string — 切片结果
(Regexp) expand
pub fn (re Regexp) expand(s &string, repl &string) string
用途
在 Regexp 上执行 expand 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
repl &string- repl(只读借用,调用方仍持有原值;类型 &string)
返回值
string — 结果字符串
struct LiteralPrefix
pub struct LiteralPrefix {
pub prefix string
pub complete bool
}
用途
承载 prefix、complete 等属性的结构体。
公开字段
prefix string- 前缀
complete bool- 字段(bool)
(Regexp) literal_prefix
pub fn (re Regexp) literal_prefix() LiteralPrefix
用途
Regexp 上的操作:执行 literal prefix 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
返回值
LiteralPrefix — 该类型的值
(Regexp) match_string
pub fn (re Regexp) match_string(s &string) bool
用途
Regexp 上的操作:执行 match string 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
返回值
bool — 是否成立
(Regexp) find
pub fn (re Regexp) find(s &string) string
用途
在 Regexp 上执行 find 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
返回值
string — 结果字符串
struct Index
pub struct Index {
pub start int
pub end int
}
用途
承载 start、end 等属性的结构体。
公开字段
start int- 字段(int)
end int- 字段(int)
(Regexp) find_index
pub fn (re Regexp) find_index(s &string) Index
用途
Regexp 上的操作:执行 find index 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
返回值
Index — 该类型的值
(Regexp) find_all_index
pub fn (re Regexp) find_all_index(s &string, n int) []Index
用途
Regexp 上的操作:执行 find all index 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
n int- 数量、长度或上限(int)
返回值
[]Index — 切片结果
(Regexp) find_all
pub fn (re Regexp) find_all(s &string, n int) []string
用途
Regexp 上的操作:执行 find all 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
n int- 数量、长度或上限(int)
返回值
[]string — 切片结果
(Regexp) split
pub fn (re Regexp) split(s &string, n int) []string
用途
Regexp 上的操作:按分隔符切开。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
n int- 数量、长度或上限(int)
返回值
[]string — 切片结果
(Regexp) find_submatch_index
pub fn (re Regexp) find_submatch_index(s &string) []Index
用途
Regexp 上的操作:执行 find submatch index 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
返回值
[]Index — 切片结果
(Regexp) replace_all_literal
pub fn (re Regexp) replace_all_literal(s &string, repl &string) string
用途
Regexp 上的操作:执行 replace all literal 操作。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
repl &string- repl(只读借用,调用方仍持有原值;类型 &string)
返回值
string — 结果字符串
(Regexp) replace_all
pub fn (re Regexp) replace_all(s &string, repl &string) string
用途
Regexp 上的操作:替换全部出现的子串。
参数
re Regexp- 本次调用使用的
Regexp 实例;按值接收。
s &string- 源字符串(只读借用,调用方仍持有原值;类型 &string)
repl &string- repl(只读借用,调用方仍持有原值;类型 &string)
返回值
string — 结果字符串