当前位置:网站首页>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
边栏推荐
- How to delete gridlines in ggplot2 (with examples)
- A high-frequency interview question from a large factory: tell me about the solution of high and low interface idempotence? Solutions are attached.
- How to adjust face input size
- Remember a simple JVM tuning experience
- 【flask入门系列】flask处理请求和处理响应
- NoSQL之Redis配置与优化
- [solution] cmake was unable to find a build program corresponding to "UNIX makefiles"
- 网上开户选哪个证券公司?网上开户是否安全么?
- 学习太极创客 — MQTT(五)发布、订阅和取消订阅
- 请指教同花顺开户选选择哪家券商比较好?网上开户是否安全么?
猜你喜欢
How to solve the problem that the iPhone 13 lock screen cannot receive the wechat notification prompt?
Audio video and CPU architecture
[machine learning] case study of college entrance examination prediction based on multiple time series
工作室第3次HarmonyOS培训笔记
Possible values for @supply in kotlin
Camtasia 2022 new ultra clear recording computer video
Oracle连接问题以及解决方案
The programmer's eight-year salary change has made netizens envious: you pay me one year's salary per month
OpenAPI 3.0 specification - Food Guide
【解决】CMake was unable to find a build program corresponding to “Unix Makefiles“.
随机推荐
Chapter I: essential information collection of penetration test
Redis configuration and optimization of NoSQL
Components and routing
网络PXE启动WinPE,支持UEFI和LEGACY引导
工作室第3次HarmonyOS培训笔记
记一个RestControll和Controller 引起的折磨BUG
[machinetranslation] - Calculation of Bleu value
Translation notes of orb-slam series papers
力扣(LeetCode)175. 组合两个表(2022.06.24)
Camtasia 2022 new ultra clear recording computer video
Please advise tonghuashun which securities firm to choose for opening an 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?
ArrayList#subList这四个坑,一不小心就中招
Use annotationdbi to convert gene names in R
Une citation classique de la nature humaine que vous ne pouvez pas ignorer
请指教同花顺开户选选择哪家券商比较好?网上开户是否安全么?
在UE内添加控制台工程(Programs)
Introduction to bloc: detailed explanation of cube
ORB-SLAM系列论文翻译笔记
Cox 回归模型