- 默认是最短匹配,只要字符串满足条件就返回。
- 如果没有匹配到,都是返回为
nil 。 - 如果需要做最长匹配,调用
Longest() 函数。 - 正则表达式功能:匹配(macth),查找(find)和替换(replace)。
- 存在长度选择的函数,传入
<0 的数字表示匹配全部。
Match,MatchReader和 MatchString
Compile 和 MushCompile
- func Compile(expr string) (*Regexp, error)
- func MustCompile(str string) *Regexp
在匹配文本时,该正则表达式会尽可能早的开始匹配,并且在匹配过程中选择回溯搜索到的第一个匹配结果。这种模式被称为leftmost-first,另外一般情况下使用
Find 和 FindAll
- func (re *Regexp) Find(b []byte) []byte
- func (re *Regexp) FindAll(b []byte, n int) [][]byte
Find返回保管正则表达式re在b中的最左侧的一个匹配结果的[]byte切片。如果没有匹配到,会返回nil,最多匹配一个。
FindString 和 FindAllString
- func (re *Regexp) FindString(s string) string
- func (re *Regexp) FindAllString(s string, n int) []string
与
FindIndex 和 FindAllIndex
-
func (re *Regexp) FindIndex(b []byte) (loc []int)
-
func (re *Regexp) FindAllIndex(b []byte, n int) [][]int
FindStringIndex 和 FindAllStringIndex
- func (re *Regexp) FindStringIndex(s string) (loc []int)
- func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
与
FindStringSubmatch 和 FindAllStringSubmatch
- func (re *Regexp) FindStringSubmatch(s string) []string
输出结果:
- func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string
和
输出结果:
FindSubmatchIndex 和 FindAllSubmatchIndex
- func (re *Regexp) FindSubmatchIndex(b []byte) []int
- func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int
计算子串在源串中的位置,已经存在(x*) 等返回结果处理,如果没有返回nil 。
另外,
并且,不会存在重合匹配的,比如说"-axxb-ab-"去匹配
输出结果:
FindStringSubmatchIndex 和 FindAllStringSubmatchIndex
- func (re *Regexp) FindStringSubmatchIndex(s string) []int
- func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
和
Longest
- func (re *Regexp) Longest()
获取最长匹配的满足条件的内容。
输出结果:
下面这种情况不会最长匹配。
Match,MatchString和MatchReader
- func (re *Regexp) Match(b []byte) bool
- func (re *Regexp) MatchString(s string) bool
- func (re *Regexp) MatchReader(r io.RuneReader) bool
判断
NumSubexp
- func (re *Regexp) NumSubexp() int
返回分组的数量。
输出结果:
ReplaceAll 和 ReplaceAllString
- func (re *Regexp) ReplaceAll(src, repl []byte) []byte
- func (re *Regexp) ReplaceAllString(src, repl string) string
输出结果:
ReplaceAllFunc 和 ReplaceAllStringFunc
- func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
- func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string
将匹配出来满足条件的
两者使用方式类似。
ReplaceAllLiteral 和 ReplaceAllLiteralString
- func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte
- func (re *Regexp) ReplaceAllLiteralString(src, repl string) string
匹配字面常量,不转换。
输出结果:
关于
Expand 和 ExpandString
- func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte
- func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte
Expand返回新生成的将template添加到dst后面的切片。在添加时,Expand会将template中的变量替换为从src匹配的结果。match应该是被FindSubmatchIndex返回的匹配结果起止位置索引。(通常就是匹配src,除非你要将匹配得到的位置用于另一个[]byte)
在template参数里,一个变量表示为格式如:
$name 或${name} 的字符串,其中name是长度>0的字母、数字和下划线的序列。一个单纯的数字字符名如$1会作为捕获分组的数字索引;其他的名字对应(?P...)语法产生的命名捕获分组的名字。超出范围的数字索引、索引对应的分组未匹配到文本、正则表达式中未出现的分组名,都会被替换为空切片。
$name格式的变量名,name会尽可能取最长序列:
$1x 等价于${1x} 而非${1}x ,$10 等价于${10} 而非${1}0 。因此$name 适用在后跟空格/换行等字符的情况,${name} 适用所有情况。
其他示例如果要在输出中插入一个字面值
'$' ,在template里可以使用$$ 。
解析网址
输出结果: