当前位置:网站首页>golang正則regexp包使用-06-其他用法(特殊字符轉換、查找正則共同前綴、切換貪婪模式、查詢正則分組個數、查詢正則分組名稱、用正則切割、查詢正則字串)
golang正則regexp包使用-06-其他用法(特殊字符轉換、查找正則共同前綴、切換貪婪模式、查詢正則分組個數、查詢正則分組名稱、用正則切割、查詢正則字串)
2022-06-26 02:59: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
边栏推荐
- 我在中山,到哪里开户比较好?网上开户是否安全么?
- Survival analysis based on ovarian data set
- arduino字符串转16进制数 大彩串口屏用。
- Arthas(阿尔萨斯) 能为你做什么?
- Install development cross process communication
- 请指教同花顺开户选选择哪家券商比较好?网上开户是否安全么?
- How do I take a screenshot of the iPad? 7 ways to take quick screenshots of iPad
- R language survival analysis
- 附加:HikariCP连接池简述;(并没有深究,只是对HikariCP连接池有个基本认识)
- Mysql常用sql语句之修改表名、删除表、获取表信息、删除指定日期的表记录
猜你喜欢
随机推荐
Create a nonlinear least squares test in R
《你不可不知的人性》經典語錄
The difference between like,%, - in database query SQL
Deep understanding of distributed cache design
Use annotationdbi to convert gene names in R
Gold three silver four~
无法上网问题解决过程
R language Markov chain Monte Carlo: practical introduction
[机器翻译]—BLEU值的计算
Which securities company should I choose to open an account online? Is it safe to open an account online?
Oracle连接问题以及解决方案
Problem solving process of no internet access
P2483-[模板]k短路/[SDOI2010]魔法猪学院【主席树,堆】
Matlab|基于BP神经网络进行电力系统短期负荷预测
Dreamcamera2 video recording, playing without sound, recording function is normal, using a third-party application for video recording, playing with sound
小 P 周刊 Vol.10
[machine learning] case study of college entrance examination prediction based on multiple time series
力扣(LeetCode)176. 第二高的薪水(2022.06.25)
UE5全局光照系統Lumen解析與優化
How to add a regression equation to a plot in R








![[机器翻译]—BLEU值的计算](/img/c3/8f98db33eb0ab5a016621d21d971e4.png)
