go语言中的正则操作
qingheluo2021-10-25清河洛477
"regexp"包实现了正则表达式搜索regexp包使用的是RE2正则表达式引擎,目的是为了保证正则表达式的性能和安全性。RE2不支持一些高级特性,如\d、\s、\w、回溯引用等,可以使用以下代替
\s [\t\n\f\r ]
\d [0-9]
\w [0-9A-Za-z_]
通用函数QuoteMeta(s string) string:返回将s中所有正则表达式元字符都进行转义后字符串
Match(pattern string, b []byte) (matched bool, err error):检查b中是否存在匹配pattern的子序列
MatchString(patt...
"regexp"包实现了正则表达式搜索
regexp包使用的是RE2正则表达式引擎,目的是为了保证正则表达式的性能和安全性。RE2不支持一些高级特性,如\d、\s、\w、回溯引用等,可以使用以下代替
\s [\t\n\f\r ] \d [0-9] \w [0-9A-Za-z_]
通用函数
QuoteMeta(s string) string:返回将s中所有正则表达式元字符都进行转义后字符串 Match(pattern string, b []byte) (matched bool, err error):检查b中是否存在匹配pattern的子序列 MatchString(pattern string, s string) (matched bool, err error):类似Match,但匹配对象是字符串 MatchReader(pattern string, r io.RuneReader) (matched bool, err error):类似Match,但匹配对象是io.RuneReader
生成正则表达式
Compile(expr string) (*Regexp, error):解析并返回一个正则表达式 匹配模式设置为leftmost-first,最短匹配,一旦匹配就立刻返回 CompilePOSIX(expr string) (*Regexp, error):解析并返回一个正则表达式 匹配模式设置为leftmost-longest,贪婪匹配,或尝试更长的匹配内容 MustCompile(str string) *Regexp:类似Compile但会在解析失败时panic MustCompilePOSIX(str string) *Regexp:类似CompilePOSIX但会在解析失败时panic
标志设置
在匹配正则时,有时需要涉及大小写是否敏感、点字符是否匹配换行符等,可以在正则字符串开头使用(?xyz)标志来设置
i 大小写敏感,默认不敏感 m ^和$额外匹配行首和行尾,默认开启 s 点字符 "." 可以匹配\n,默认关闭 U 禁止贪婪匹配,默认关闭 要开启的功能写在前面,要关闭的使用减号(-)连接写在后面,如 (?IsU) 表示开启指定功能 (?-Im) 表示关闭指定功能 (?I-m) 表示开启大小写敏感,关闭^和$匹配行首和行尾
type Regexp struct {}:代表一个编译好的正则表达式,可以被多线程安全地同时使用
Regexp的常用方法
String() string:返回用于编译成正则表达式的字符串 LiteralPrefix() (prefix string, complete bool) 返回创建该正则对象时的字符串中开头至第一个元字符之间的字符串 如"abc.\*"会返回字符串"abc" 如果返回值等于创建对象的字符串,即创建字符串中不存在任何元字符,第二个参数返回true,否则返回false NumSubexp() int:返回该正则表达式中捕获分组的数量 SubexpNames() []string: 返回正则表达式中捕获分组的名字,第一个元素为空字符串,后续为捕获的分组名称,所以第一个分组的索引值为1 有几个捕获分组就捕获几个,当使用“(?<name>exp)”进行了分组命名返回该name名称 如果没有对分组进行命名返回空字符串 Longest():让正则表达式在之后的搜索中都采用"leftmost-longest"模式(贪婪匹配) Match(b []byte) bool:检查b中是否存在匹配pattern的子序列 MatchString(s string) bool:类似Match,但匹配对象是字符串 MatchReader(r io.RuneReader) bool:类似Match,但匹配对象是io.RuneReader 获取首次匹配信息 Find(b []byte) []byte:返回第一个匹配结果的[]byte切片。没有匹配返回nil FindString(s string) string:返回第一个匹配结果的字符串。没有匹配返回"" FindIndex(b []byte) (loc []int) 返回第一个匹配的起始位置的切片(len(loc)==2) b[loc[0]:loc[1]]即表示匹配到的结果 FindStringIndex(s string) (loc []int) FindReaderIndex(r io.RuneReader) (loc []int) FindSubmatch(b []byte) [][]byte:返回第一个匹配结果以及(可能有的)分组匹配的结果的[][]byte切片,第一个元素为完整匹配 FindStringSubmatch(s string) []string:返回第一个匹配结果以及(可能有的)分组匹配的结果的[]string切片 FindSubmatchIndex(b []byte) []int:返回第一个匹配结果以及(可能有的)分组匹配的结果的的起止位置的切片 FindStringSubmatchIndex(s string) []int FindReaderSubmatchIndex(r io.RuneReader) []int 获取所有匹配信息 以下方法中第二个参数n表示最多匹配次数,为负数表示匹配最大次数 FindAll(b []byte, n int) [][]byte:返回所有不重叠的匹配结果的[][]byte切片 FindAllString(s string, n int) []string FindAllIndex(b []byte, n int) [][]int:返回所有不重叠的匹配结果的起止位置的切片 FindAllStringIndex(s string, n int) [][]int FindAllSubmatch(b []byte, n int) [][][]byte:返回所有不重叠的匹配结果及其对应的(可能有的)分组匹配的结果的[][][]byte切片 FindAllStringSubmatch(s string, n int) [][]string FindAllSubmatchIndex(b []byte, n int) [][]int 返回所有不重叠的匹配结果及其对应的(可能有的)分组匹配的结果的起止位置的切片 第一层表示第几个匹配结果,完整匹配和分组匹配的起止位置在第二层 FindAllStringSubmatchIndex(s string, n int) [][]int 字符串分割 Split(s string, n int) []string:将re在s中匹配到的结果作为分隔符将s分割的字符串的切片,n指定返回的子字符串的数量 n > 0 : 返回最多n个子字符串,最后一个子字符串是剩余未进行分割的部分 n == 0: 返回nil n < 0 : 返回完整分割后的字符串切片 字符串连接 Expand(dst []byte, template []byte, src []byte, match []int) []byte 将template中的变量替换成从src匹配的结果,然后将替换后的template添加到dst后面 match是被FindSubmatchIndex返回的匹配结果起止位置索引 变量表示为格式如:$name或${name}的字符串,其中name是长度>0的字母、数字和下划线的序列 一个单纯的数字字符名如$1会作为捕获分组的数字索引 其他的名字对应命名捕获分组的名字 超出范围的数字索引、索引对应的分组未匹配到文本、正则表达式中未出现的分组名,都会被替换为空切片 ExpandString(dst []byte, template string, src string, match []int) []byte 字符串替换 ReplaceAllLiteral(src, repl []byte) []byte:返回将src中所有re的匹配结果都替换为repl的一个拷贝 ReplaceAllLiteralString(src, repl string) string ReplaceAll(src, repl []byte) []byte 返回将src中所有re的匹配结果都替换为repl的一个拷贝 在替换时,repl中的‘$‘符号会进行解释和替换,如$1会被替换为第一个分组匹配结果 ReplaceAllString(src, repl string) string ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte 返回src的一个拷贝,将src中所有re的匹配结果(设为matched)都替换为repl(matched) ReplaceAllStringFunc(src string, repl func(string) string) string