当前位置:网站首页>The golang regular regexp package uses -06- other usages (special character conversion, finding the regular common prefix, switching greedy mode, querying the number of regular groups, querying the na
The golang regular regexp package uses -06- other usages (special character conversion, finding the regular common prefix, switching greedy mode, querying the number of regular groups, querying the na
2022-06-26 02:59:00 【Development, operation and maintenance Xuande company】
List of articles
- 1. QuoteMeta() function ( Special character conversion )
- 2. LiteralPrefix() Method ( Find the regular common prefix )
- 3. Longest() Method ( Switch greedy mode )
- 4. NumSubexp() Method ( Query the number of groups in the regular )
- 5. Split() Method ( With regular cut )
- 6. String() Method ( take *regexp.Regexp In the instance, the regular pattern is string Output )
- 7. SubexpNames() Method ( Returns the regular group name )
1. QuoteMeta() function ( Special character conversion )
Convert special characters of regular expressions .
grammar
func QuoteMeta(s string) string
Complete example
package main
import (
"fmt"
"regexp"
)
func main() {
pattern := " Xuande .*?"
myString := regexp.QuoteMeta(pattern)
fmt.Println(myString)
}
- result
Xuande \.\*\?
2. LiteralPrefix() Method ( Find the regular common prefix )
grammar
func (re *Regexp) LiteralPrefix() (prefix string, complete bool)
prefix: Common prefix
complete: If prefix Is the regular expression itself , Then return to true, Otherwise return to false
Complete example
- Code
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())
}
- result
Hello false
Hello true
false
Above
- Results a , Common prefix
helloReturned ,falseThe prefix is not the regular expression string itself- Result two , Common prefix
helloReturned ,trueThe prefix is the regular expression string itself- Result three , No common prefix
3. Longest() Method ( Switch greedy mode )
grammar
func (re *Regexp) Longest()
Complete example
package main
import (
"fmt"
"regexp"
)
func main() {
s := `10.10.239.11`
reg := regexp.MustCompile(".*?\\.")
fmt.Printf("%s\n", reg.FindString(s))
reg.Longest() // Switch greedy mode
fmt.Printf("%s\n", reg.FindString(s))
}
- result
10.
10.10.239.
4. NumSubexp() Method ( Query the number of groups in the regular )
grammar
func (re *Regexp) NumSubexp() int
Complete example
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("(\\d+)\\.(\\d+)")
n := reg.NumSubexp()
fmt.Printf(" The number of groups in the regular is :%d",n)
}
- result
The number of groups in the regular is :2
5. Split() Method ( With regular cut )
grammar
func (re *Regexp) Split(s string, n int) []string
Complete example
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile(`\.`)
myString := "www.xishu.com"
result := reg.Split(myString, -1)
fmt.Printf(" result :%q\n",result )
}
- result
result :["www" "xishu" "com"]
6. String() Method ( take *regexp.Regexp In the instance, the regular pattern is string Output )
grammar
func (re *Regexp) String() string
Complete example
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\d+$")
pattern := reg.String()
fmt.Printf("%s\n", pattern)
}
- result
\d+$
7. SubexpNames() Method ( Returns the regular group name )
grammar
func (re *Regexp) SubexpNames() []string
Name regular groups :
(?P<NAME>)
Complete example
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)
}
- result
[ first second third fourth]
https://blog.csdn.net/qq_41629756/article/details/100516635
边栏推荐
- 分布式电商项目 谷粒商城 学习笔记<3>
- 如何在 ggplot2 中向绘图中添加表格
- Lumen Analysis and Optimization of ue5 global Lighting System
- Which securities company should I choose to open an account online? Is it safe to open an account online?
- Components and routing
- OpenAPI 3.0 specification - Food Guide
- 55 pictures make you feel a bit B-tree at one time
- 请指教同花顺软件究竟是什么?在线开户安全么?
- Analysis and optimization of ue5 global illumination system lumen
- 【flask入门系列】flask处理请求和处理响应
猜你喜欢

Introduction to bloc: detailed explanation of cube

GD32 ADC采集电压

Possible values for @supply in kotlin

Gd32 ADC acquisition voltage

UTONMOS:以数字藏品助力华夏文化传承和数字科技发展

音视频与CPU架构

55 pictures make you feel a bit B-tree at one time

数据库的授权

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.
随机推荐
Multi surveyor Gongshu campus Xiao sir_ Page error in Jenkins
Little p weekly Vol.10
Technology is to be studied
Oracle连接问题以及解决方案
Cox regression model
《你不可不知的人性》經典語錄
在UE内添加控制台工程(Programs)
【QT】自定义控件-空气质量仪表盘
Network PXE starts winpe and supports UEFI and legacy boot
OpenAPI 3.0 specification - Food Guide
Is it safe to open an online stock account?
附加:HikariCP连接池简述;(并没有深究,只是对HikariCP连接池有个基本认识)
无法上网问题解决过程
数据库查询语句SQL中like、%、-的区别
Chapter I: essential information collection of penetration test
UE5全局光照系統Lumen解析與優化
UTONMOS坚持“藏品、版权”双优原则助力传统文化高质量发展
I am in Zhongshan. Where can I open an account? Is it safe to open an account online?
High availability in Internet three highs (high concurrency, high performance and high availability)
学习太极创客 — MQTT(四)服务端连接操作