当前位置:网站首页>golang正则regexp包使用-06-其他用法(特殊字符转换、查找正则共同前缀、切换贪婪模式、查询正则分组个数、查询正则分组名称、用正则切割、查询正则字串)
golang正则regexp包使用-06-其他用法(特殊字符转换、查找正则共同前缀、切换贪婪模式、查询正则分组个数、查询正则分组名称、用正则切割、查询正则字串)
2022-06-26 02:23:00 【开发运维玄德公】
文章目录
1. QuoteMeta() 函数(特殊字符转换)
将正则表达式的特殊字符转换。
语法
func QuoteMeta(s string) string
完整示例
package main
import (
"fmt"
"regexp"
)
func main() {
pattern := "玄德.*?"
myString := regexp.QuoteMeta(pattern)
fmt.Println(myString)
}
- 结果
玄德\.\*\?
2. LiteralPrefix()方法(查找正则共同前缀)
语法
func (re *Regexp) LiteralPrefix() (prefix string, complete bool)
prefix:共同拥有的前缀
complete:如果 prefix 就是正则表达式本身,则返回 true,否则返回 false
完整示例
- 代码
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile(`Hello[\w\s]+`)
fmt.Println(reg.LiteralPrefix())
reg = regexp.MustCompile(`Hello`)
fmt.Println(reg.LiteralPrefix())
reg = regexp.MustCompile(`.*Hello`)
fmt.Println(reg.LiteralPrefix())
}
- 结果
Hello false
Hello true
false
如上
- 结果一,共同前缀
hello被返回,false说明该前缀不是正则表达式字串本身- 结果二,共同前缀
hello被返回,true说明该前缀是正则表达式字串本身- 结果三,没有共同前缀
3. Longest()方法(切换贪婪模式)
语法
func (re *Regexp) Longest()
完整示例
package main
import (
"fmt"
"regexp"
)
func main() {
s := `10.10.239.11`
reg := regexp.MustCompile(".*?\\.")
fmt.Printf("%s\n", reg.FindString(s))
reg.Longest() // 切换贪婪模式
fmt.Printf("%s\n", reg.FindString(s))
}
- 结果
10.
10.10.239.
4. NumSubexp()方法(查询正则中分组个数)
语法
func (re *Regexp) NumSubexp() int
完整示例
- 代码
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("(\\d+)\\.(\\d+)")
n := reg.NumSubexp()
fmt.Printf("正则中分组个数为:%d",n)
}
- 结果
正则中分组个数为:2
5. Split() 方法(用正则切割)
语法
func (re *Regexp) Split(s string, n int) []string
完整示例
- 代码
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile(`\.`)
myString := "www.xishu.com"
result := reg.Split(myString, -1)
fmt.Printf("结果:%q\n",result )
}
- 结果
结果:["www" "xishu" "com"]
6. String()方法 (将*regexp.Regexp实例中正则以string输出)
语法
func (re *Regexp) String() string
完整示例
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\d+$")
pattern := reg.String()
fmt.Printf("%s\n", pattern)
}
- 结果
\d+$
7. SubexpNames() 方法(返回正则分组名)
语法
func (re *Regexp) SubexpNames() []string
给正则分组命名:
(?P<NAME>)
完整示例
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("(?P<first>\\d+)\\.(?P<second>\\d+)\\.(?P<third>\\d+)\\.(?P<fourth>\\d+)$")
namesList := reg.SubexpNames()
fmt.Printf("%+v\n", namesList)
}
- 结果
[ first second third fourth]
https://blog.csdn.net/qq_41629756/article/details/100516635
边栏推荐
- 无法上网问题解决过程
- R 语言降维的 PCA 与自动编码器
- 分布式电商项目 谷粒商城 学习笔记<3>
- R language survival analysis
- How to adjust face input size
- Dreamcamera2 video recording, playing without sound, recording function is normal, using a third-party application for video recording, playing with sound
- Install development cross process communication
- Gold three silver four~
- 限制输入字符长度length英文1个字符中文2个字符
- golang--channal与select
猜你喜欢

Camtasia 2022全新版超清录制电脑视频

A high-frequency interview question from a large factory: tell me about the solution of high and low interface idempotence? Solutions are attached.

应届毕业生谈毕业的故事
![[machine learning] case study of college entrance examination prediction based on multiple time series](/img/13/7368a169d07e01d86a2d2c0207ccca.png)
[machine learning] case study of college entrance examination prediction based on multiple time series
![[solution] the blue screen restart problem of the virtual machine started by the VMware of Lenovo Savior](/img/c3/892ce2f45abea7140df98cabc1431b.png)
[solution] the blue screen restart problem of the virtual machine started by the VMware of Lenovo Savior

GD32 ADC采集电压

Depth first traversal of Graphs

工作室第3次HarmonyOS培训笔记

Deep understanding of distributed cache design

OpenAPI 3.0 specification - Food Guide
随机推荐
Camtasia 2022 new ultra clear recording computer video
How to check and cancel subscription auto renewal on iPhone or iPad
请指教同花顺开户选选择哪家券商比较好?网上开户是否安全么?
arduino字符串转16进制数 大彩串口屏用。
UTONMOS坚持“藏品、版权”双优原则助力传统文化高质量发展
Install development cross process communication
scrapy返回400
Technology is to be studied
应届毕业生谈毕业的故事
[flask introduction series] flask processing request and response
ArrayList#subList这四个坑,一不小心就中招
P2483-[模板]k短路/[SDOI2010]魔法猪学院【主席树,堆】
golang--channal与select
I am in Zhongshan. Where can I open an account? Is it safe to open an account online?
College Students' big factory road
数据库的授权
How to open a stock account? Is it safe to open an account online?
Is it safe to open a securities account at the school of Finance and business in 2022?
Learn Tai Chi Maker - mqtt (IV) server connection operation
Explain the JVM clearly at one time and don't be asked by the interviewer again