- Go语言入门
- Go语言开发环境安装配置
- Go语言程序结构
- Go语言基础语法
- Go语言数据类型
- Go语言变量
- Go语言常量
- Go语言运算符
- Go语言条件和决策
- Go语言循环
- Go语言函数
- Go语言作用域规则
- Go语言字符串
- Go语言数组
- Go语言指针
- Go语言结构体
- Go语言切片
- Go语言范围(range)
- Go语言映射
- Go语言递归
- Go语言类型转换
- Go语言接口
- Go语言错误处理
-
Go编程代码实例
- Hello World程序实例
- Go变量实例
- Go常量实例
- Go for循环语句实例
- Go if/else语句实例
- Go switch语句实例
- Go切片实例
- Go范围实例
- Go函数实例
- Go函数多个返回值实例
- Go可变参数的函数实例
- Go闭包(匿名函数)实例
- Go函数递归实例
- Go指针实例
- Go指针实例
- Go接口实例
- Go错误实例
- Go程序实例
- Go通道实例
- Go通道缓冲实例
- Go通道同步实例
- Go通道路线实例
- Go Select实例
- Go超时(timeouts)实例
- Go非阻塞通道操作实例
- Go关闭通道实例
- Go通道范围实例
- Go计时器实例
- Go断续器实例
- Go工作池实例
- Go速率限制实例
- Go原子计数器实例
- Go互斥体实例
- Go有状态的goroutines实例
- Go排序实例
- Go按自定义函数排序实例
- Go panic错误处理实例
- Go延迟(defer)实例
- Go集合函数实例
- Go字符串函数实例
- Go字符串格式化实例
- Go正则表达式实例
- Go JSON实例
- Go时间日期实例
- Go时代(Epoch)实例
- Go时间格式化/解析实例
- Go随机数实例
- Go数字解析实例
- Go URL解析实例
- Go SHA1哈希实例
- Go Base64编码实例
- Go读取文件实例
- Go写文件实例
- Go行过滤器实例
- Go命令行参数实例
- Go命令行标志实例
- Go环境变量实例
- Go执行过程实例
- Go信号实例
- Go退出程序实例
Go正则表达式实例
Go提供对正则表达式的内置支持。 下面是Go中常见的regexp
相关任务的一些例子。
具体的每个函数,可参考示例中的代码 -
所有的示例代码,都放在
F:\worksp\golang
目录下。安装Go编程环境请参考:/tutorial/detail-5562.html
string-formatting.go
的完整代码如下所示 -
package main import "bytes" import "fmt" import "regexp" func main() { // This tests whether a pattern matches a string. match, _ := regexp.MatchString("p([a-z]+)ch", "peach") fmt.Println(match) // Above we used a string pattern directly, but for // other regexp tasks you'll need to `Compile` an // optimized `Regexp` struct. r, _ := regexp.Compile("p([a-z]+)ch") // Many methods are available on these structs. Here's // a match test like we saw earlier. fmt.Println(r.MatchString("peach")) // This finds the match for the regexp. fmt.Println(r.FindString("peach punch")) // This also finds the first match but returns the // start and end indexes for the match instead of the // matching text. fmt.Println(r.FindStringIndex("peach punch")) // The `Submatch` variants include information about // both the whole-pattern matches and the submatches // within those matches. For example this will return // information for both `p([a-z]+)ch` and `([a-z]+)`. fmt.Println(r.FindStringSubmatch("peach punch")) // Similarly this will return information about the // indexes of matches and submatches. fmt.Println(r.FindStringSubmatchIndex("peach punch")) // The `All` variants of these functions apply to all // matches in the input, not just the first. For // example to find all matches for a regexp. fmt.Println(r.FindAllString("peach punch pinch", -1)) // These `All` variants are available for the other // functions we saw above as well. fmt.Println(r.FindAllStringSubmatchIndex( "peach punch pinch", -1)) // Providing a non-negative integer as the second // argument to these functions will limit the number // of matches. fmt.Println(r.FindAllString("peach punch pinch", 2)) // Our examples above had string arguments and used // names like `MatchString`. We can also provide // `[]byte` arguments and drop `String` from the // function name. fmt.Println(r.Match([]byte("peach"))) // When creating constants with regular expressions // you can use the `MustCompile` variation of // `Compile`. A plain `Compile` won't work for // constants because it has 2 return values. r = regexp.MustCompile("p([a-z]+)ch") fmt.Println(r) // The `regexp` package can also be used to replace // subsets of strings with other values. fmt.Println(r.ReplaceAllString("a peach", "<fruit>")) // The `Func` variant allows you to transform matched // text with a given function. in := []byte("a peach") out := r.ReplaceAllFunc(in, bytes.ToUpper) fmt.Println(string(out)) }
执行上面代码,将得到以下输出结果 -
F:\worksp\golang>go run regular-expressions.go true true peach [0 5] [peach ea] [0 5 1 3] [peach punch pinch] [[0 5 1 3] [6 11 7 9] [12 17 13 15]] [peach punch] true p([a-z]+)ch a <fruit> a PEACH
上一篇:Go字符串格式化实例
下一篇:Go JSON实例