当前位置:网站首页>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
边栏推荐
- How to prompt
- Arduino string to hexadecimal number for large color serial port screen.
- Can the main RF circuit be removed for projects that do not need the main RF?
- Use annotationdbi to convert gene names in R
- Remember a simple JVM tuning experience
- GD32 ADC采集电压
- Arthas(阿尔萨斯) 能为你做什么?
- Little p weekly Vol.10
- The "more" option of Spreadtrum platform dearmcamera2 is removed
- How to check and cancel subscription auto renewal on iPhone or iPad
猜你喜欢
随机推荐
请指教同花顺软件究竟是什么?在线开户安全么?
Interpreting Oracle
Oracle练习
力扣(LeetCode)175. 组合两个表(2022.06.24)
学习太极创客 — MQTT(五)发布、订阅和取消订阅
在 R 中创建非线性最小二乘检验
Is it safe to open an online stock account?
golang--channal与select
Multi surveyor Gongshu campus Xiao sir_ Page error in Jenkins
Version management tool usage
OpenAPI 3.0 specification - Food Guide
What can Arthas do for you?
组件与路由
Learn from Taiji makers - mqtt (V) publish, subscribe and unsubscribe
Explain the JVM clearly at one time and don't be asked by the interviewer again
How to prompt
用指南针交易股票安全吗?指南针是如何交易股票的,需要开户吗
在UE内添加控制台工程(Programs)
vulhub复现一 activemq
Survival analysis based on ovarian data set








