------------------------------------------------------------

// 函数

// 判断在 b(s、r)中能否找到 pattern 所匹配的字符串
func Match(pattern string, b []byte) (matched bool, err error)
func MatchString(pattern string, s string) (matched bool, err error)
func MatchReader(pattern string, r io.RuneReader) (matched bool, err error) // 将 s 中的正则表达式元字符转义成普通字符。
func QuoteMeta(s string) string ------------------------------ // 示例:MatchString、QuoteMeta
func main() {
pat := `(((abc.)def.)ghi)`
src := `abc-def-ghi abc+def+ghi` fmt.Println(regexp.MatchString(pat, src))
// true <nil> fmt.Println(regexp.QuoteMeta(pat))
// \(\(\(abc\.\)def\.\)ghi\)
} ------------------------------------------------------------ // Regexp 代表一个编译好的正则表达式,我们这里称之为正则对象。正则对象可以
// 在文本中查找匹配的内容。
//
// Regexp 可以安全的在多个例程中并行使用。
type Regexp struct { ... } ------------------------------ // 编译 // 将正则表达式编译成一个正则对象(使用 PERL 语法)。
// 该正则对象会采用“leftmost-first”模式。选择第一个匹配结果。
// 如果正则表达式语法错误,则返回错误信息。
func Compile(expr string) (*Regexp, error) // 将正则表达式编译成一个正则对象(正则语法限制在 POSIX ERE 范围内)。
// 该正则对象会采用“leftmost-longest”模式。选择最长的匹配结果。
// POSIX 语法不支持 Perl 的语法格式:\d、\D、\s、\S、\w、\W
// 如果正则表达式语法错误,则返回错误信息。
func CompilePOSIX(expr string) (*Regexp, error) // 功能同上,但会在解析失败时 panic
func MustCompile(str string) *Regexp
func MustCompilePOSIX(str string) *Regexp // 让正则表达式在之后的搜索中都采用“leftmost-longest”模式。
func (re *Regexp) Longest() // 返回编译时使用的正则表达式字符串
func (re *Regexp) String() string // 返回正则表达式中分组的数量
func (re *Regexp) NumSubexp() int // 返回正则表达式中分组的名字
// 第 0 个元素表示整个正则表达式的名字,永远是空字符串。
func (re *Regexp) SubexpNames() []string // 返回正则表达式必须匹配到的字面前缀(不包含可变部分)。
// 如果整个正则表达式都是字面值,则 complete 返回 true。
func (re *Regexp) LiteralPrefix() (prefix string, complete bool) ------------------------------ // 示例:第一匹配和最长匹配
func main() {
b := []byte("abc1def1")
pat := `abc1|abc1def1`
reg1 := regexp.MustCompile(pat) // 第一匹配
reg2 := regexp.MustCompilePOSIX(pat) // 最长匹配
fmt.Printf("%s\n", reg1.Find(b)) // abc1
fmt.Printf("%s\n", reg2.Find(b)) // abc1def1 b = []byte("abc1def1")
pat = `(abc|abc1def)*1`
reg1 = regexp.MustCompile(pat) // 第一匹配
reg2 = regexp.MustCompilePOSIX(pat) // 最长匹配
fmt.Printf("%s\n", reg1.Find(b)) // abc1
fmt.Printf("%s\n", reg2.Find(b)) // abc1def1
} ------------------------------ // 示例:正则信息
func main() {
pat := `(abc)(def)(ghi)`
reg := regexp.MustCompile(pat) // 获取正则表达式字符串
fmt.Println(reg.String()) // (abc)(def)(ghi) // 获取分组数量
fmt.Println(reg.NumSubexp()) // 3 fmt.Println() // 获取分组名称
pat = `(?P<Name1>abc)(def)(?P<Name3>ghi)`
reg = regexp.MustCompile(pat) for i := 0; i <= reg.NumSubexp(); i++ {
fmt.Printf("%d: %q\n", i, reg.SubexpNames()[i])
}
// 0: ""
// 1: "Name1"
// 2: ""
// 3: "Name3" fmt.Println() // 获取字面前缀
pat = `(abc1)(abc2)(abc3)`
reg = regexp.MustCompile(pat)
fmt.Println(reg.LiteralPrefix()) // abc1abc2abc3 true pat = `(abc1)|(abc2)|(abc3)`
reg = regexp.MustCompile(pat)
fmt.Println(reg.LiteralPrefix()) // false pat = `abc1|abc2|abc3`
reg = regexp.MustCompile(pat)
fmt.Println(reg.LiteralPrefix()) // abc false
} ------------------------------ // 判断 // 判断在 b(s、r)中能否找到匹配的字符串
func (re *Regexp) Match(b []byte) bool
func (re *Regexp) MatchString(s string) bool
func (re *Regexp) MatchReader(r io.RuneReader) bool ------------------------------ // 查找 // 返回第一个匹配到的结果(结果以 b 的切片形式返回)。
func (re *Regexp) Find(b []byte) []byte // 返回第一个匹配到的结果及其分组内容(结果以 b 的切片形式返回)。
// 返回值中的第 0 个元素是整个正则表达式的匹配结果,后续元素是各个分组的
// 匹配内容,分组顺序按照“(”的出现次序而定。
func (re *Regexp) FindSubmatch(b []byte) [][]byte // 功能同 Find,只不过返回的是匹配结果的首尾下标,通过这些下标可以生成切片。
// loc[0] 是结果切片的起始下标,loc[1] 是结果切片的结束下标。
func (re *Regexp) FindIndex(b []byte) (loc []int) // 功能同 FindSubmatch,只不过返回的是匹配结果的首尾下标,通过这些下标可以生成切片。
// loc[0] 是结果切片的起始下标,loc[1] 是结果切片的结束下标。
// loc[2] 是分组1切片的起始下标,loc[3] 是分组1切片的结束下标。
// loc[4] 是分组2切片的起始下标,loc[5] 是分组2切片的结束下标。
// 以此类推
func (re *Regexp) FindSubmatchIndex(b []byte) (loc []int) ------------------------------ // 示例:Find、FindSubmatch
func main() {
pat := `(((abc.)def.)ghi)`
reg := regexp.MustCompile(pat) src := []byte(`abc-def-ghi abc+def+ghi`) // 查找第一个匹配结果
fmt.Printf("%s\n", reg.Find(src)) // abc-def-ghi fmt.Println() // 查找第一个匹配结果及其分组字符串
first := reg.FindSubmatch(src)
for i := 0; i < len(first); i++ {
fmt.Printf("%d: %s\n", i, first[i])
}
// 0: abc-def-ghi
// 1: abc-def-ghi
// 2: abc-def-
// 3: abc-
} ------------------------------ // 示例:FindIndex、FindSubmatchIndex
func main() {
pat := `(((abc.)def.)ghi)`
reg := regexp.MustCompile(pat) src := []byte(`abc-def-ghi abc+def+ghi`) // 查找第一个匹配结果
matched := reg.FindIndex(src)
fmt.Printf("%v\n", matched) // [0 11]
m := matched[0]
n := matched[1]
fmt.Printf("%s\n\n", src[m:n]) // abc-def-ghi // 查找第一个匹配结果及其分组字符串
matched = reg.FindSubmatchIndex(src)
fmt.Printf("%v\n", matched) // [0 11 0 11 0 8 0 4]
for i := 0; i < len(matched)/2; i++ {
m := matched[i*2]
n := matched[i*2+1]
fmt.Printf("%s\n", src[m:n])
}
// abc-def-ghi
// abc-def-ghi
// abc-def-
// abc-
} ------------------------------ // 功能同上,只不过返回多个匹配的结果,而不只是第一个。
// n 是查找次数,负数表示不限次数。
func (re *Regexp) FindAll(b []byte, n int) [][]byte
func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte func (re *Regexp) FindAllIndex(b []byte, n int) [][]int
func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int ------------------------------ // 示例:FindAll、FindAllSubmatch
func main() {
pat := `(((abc.)def.)ghi)`
reg := regexp.MustCompile(pat) s := []byte(`abc-def-ghi abc+def+ghi`) // 查找所有匹配结果
for _, one := range reg.FindAll(s, -1) {
fmt.Printf("%s\n", one)
}
// abc-def-ghi
// abc+def+ghi // 查找所有匹配结果及其分组字符串
all := reg.FindAllSubmatch(s, -1)
for i := 0; i < len(all); i++ {
fmt.Println()
one := all[i]
for i := 0; i < len(one); i++ {
fmt.Printf("%d: %s\n", i, one[i])
}
}
// 0: abc-def-ghi
// 1: abc-def-ghi
// 2: abc-def-
// 3: abc- // 0: abc+def+ghi
// 1: abc+def+ghi
// 2: abc+def+
// 3: abc+
} ------------------------------ // 功能同上,只不过在字符串中查找
func (re *Regexp) FindString(s string) string
func (re *Regexp) FindStringSubmatch(s string) []string func (re *Regexp) FindStringIndex(s string) (loc []int)
func (re *Regexp) FindStringSubmatchIndex(s string) []int func (re *Regexp) FindAllString(s string, n int) []string
func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int // 功能同上,只不过在 io.RuneReader 中查找。
func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int)
func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int ------------------------------ // 替换(不会修改参数,结果是参数的副本) // 将 src 中匹配的内容替换为 repl(repl 中可以使用 $1 $name 等分组引用符)。
func (re *Regexp) ReplaceAll(src, repl []byte) []byte // 将 src 中匹配的内容经过 repl 函数处理后替换回去。
func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte // 将 src 中匹配的内容替换为 repl(repl 为字面值,不解析其中的 $1 $name 等)。
func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte // 功能同上,只不过在字符串中查找。
func (re *Regexp) ReplaceAllString(src, repl string) string
func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string
func (re *Regexp) ReplaceAllLiteralString(src, repl string) string // Expand 要配合 FindSubmatchIndex 一起使用。FindSubmatchIndex 在 src 中进行
// 查找,将结果存入 match 中。这样就可以通过 src 和 match 得到匹配的字符串。
// template 是替换内容,可以使用分组引用符 $1、$2、$name 等。Expane 将其中的分
// 组引用符替换为前面匹配到的字符串。然后追加到 dst 的尾部(dst 可以为空)。
// 说白了 Expand 就是一次替换过程,只不过需要 FindSubmatchIndex 的配合。
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
func main() {
pat := `(((abc.)def.)ghi)`
reg := regexp.MustCompile(pat) src := []byte(`abc-def-ghi abc+def+ghi`)
template := []byte(`$0 $1 $2 $3`) // 替换第一次匹配结果
match := reg.FindSubmatchIndex(src)
fmt.Printf("%v\n", match) // [0 11 0 11 0 8 0 4]
dst := reg.Expand(nil, template, src, match)
fmt.Printf("%s\n\n", dst)
// abc-def-ghi abc-def-ghi abc-def- abc- // 替换所有匹配结果
for _, match := range reg.FindAllSubmatchIndex(src, -1) {
fmt.Printf("%v\n", match)
dst := reg.Expand(nil, template, src, match)
fmt.Printf("%s\n", dst)
}
// [0 11 0 11 0 8 0 4]
// abc-def-ghi abc-def-ghi abc-def- abc-
// [12 23 12 23 12 20 12 16]
// abc+def+ghi abc+def+ghi abc+def+ abc+
} ------------------------------ // 其它 // 以 s 中的匹配结果作为分割符将 s 分割成字符串列表。
// n 是分割次数,负数表示不限次数。
func (re *Regexp) Split(s string, n int) []string // 将当前正则对象复制一份。在多例程中使用同一正则对象时,给每个例程分配一个
// 正则对象的副本,可以避免多例程对单个正则对象的争夺锁定。
func (re *Regexp) Copy() *Regexp ------------------------------------------------------------

Golang学习 - regexp 包的更多相关文章

  1. Golang学习 - sort 包

    ------------------------------------------------------------ // 满足 Interface 接口的类型可以被本包的函数进行排序. type ...

  2. Golang学习 - reflect 包

    ------------------------------------------------------------ 在 reflect 包中,主要通过两个函数 TypeOf() 和 ValueO ...

  3. Golang学习 - io 包

    ------------------------------------------------------------ 先说一下接口,Go 语言中的接口很简单,在 Go 语言的 io 包中有这样一个 ...

  4. Golang学习 - unsafe 包

    ------------------------------------------------------------ 指针类型: *类型:普通指针,用于传递对象地址,不能进行指针运算. unsaf ...

  5. Golang学习 - errors 包

    ------------------------------------------------------------ Go 语言使用 error 类型来返回函数执行过程中遇到的错误,如果返回的 e ...

  6. Golang学习 - bytes 包

    ------------------------------------------------------------ 对于传入 []byte 的函数,都不会修改传入的参数,返回值要么是参数的副本, ...

  7. Golang学习 - bufio 包

    ------------------------------------------------------------ // bufio 包实现了带缓存的 I/O 操作 -------------- ...

  8. Golang学习 - strings 包

    ------------------------------------------------------------ strings 包与 bytes 包中的函数用法基本一样,不再赘述. 只对 R ...

  9. Golang学习 - builtin 包

    Go builtin包提供了go预先声明的函数.变量等的文档.这些函数变量等的实现其实并不是在builtin包里,只是为了方便文档组织. 这些内置的变量.函数.类型无需引入包即可使用. 默认提供的有: ...

随机推荐

  1. HDU ACM 1325 / POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. 转】使用Maven编译项目遇到——“maven编码gbk的不可映射字符”解决办法

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4239006.html 感谢! 一.问题描述 今天在MyEclipse中使用Maven编译项目源代码时,结果如下了 ...

  3. 二、 C#调用存储过程

    个人比较喜欢使用第二种传递参数的方法 1. 调用的方法 public DataTable ExceStoredProcedure (string strCom, SqlParameter[] comm ...

  4. 每天学一点-Jquery判断checkbox是否为选中状态

    if ($("#ctl00_ContentPlaceHolder1_IsLimitedService").attr("checked") ==true)

  5. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

  6. mysql kill操作

    KILL语法 KILL [CONNECTION | QUERY] thread_id 每个与mysqld的连接都在一个独立的线程里运行,您可以使用SHOW PROCESSLIST语句查看哪些线程正在运 ...

  7. CCF 201312-2 ISBN号码 (水题)

    问题描述 每一本正式出版的图书都有一个ISBN号码与 之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后 ...

  8. Jackson 高性能的JSON处理 ObjectMapper

    http://blog.csdn.net/wangyang2698341/article/details/8223929 今天自行研究了下json ,感觉非常好用,经过测试比google的GSON快多 ...

  9. Ehcache(08)——可阻塞的Cache——BlockingCache

    http://haohaoxuexi.iteye.com/blog/2119737 可阻塞的Cache—BlockingCache 在上一节我们提到了显示使用Ehcache锁的问题,其实我们还可以隐式 ...

  10. 从零开始学C++之虚函数与多态(二):纯虚函数、抽象类、虚析构函数

    一.纯虚函数 虚函数是实现多态性的前提 需要在基类中定义共同的接口 接口要定义为虚函数 如果基类的接口没办法实现怎么办? 如形状类Shape 解决方法 将这些接口定义为纯虚函数 在基类中不能给出有意义 ...