当前位置:网站首页>Golang regexp package use - 06 - other usage (Special Character conversion, find regular Common prefix, Switch gourmand mode, query regular Group NUMBER, query regular Group name, cut with regular, qu
Golang regexp package use - 06 - other usage (Special Character conversion, find regular Common prefix, Switch gourmand mode, query regular Group NUMBER, query regular Group name, cut with regular, qu
2022-06-26 02:59:00 【Exploitation et entretien Xuande Gong】
Catalogue des articles
- 1. QuoteMeta() Fonctions(Conversion spéciale de caractères)
- 2. LiteralPrefix()Méthodes(Trouver un préfixe commun régulier)
- 3. Longest()Méthodes(Basculer le mode avide)
- 4. NumSubexp()Méthodes(Nombre de groupes dans la requête régulière)
- 5. Split() Méthodes(Coupe régulière)
- 6. String()Méthodes (Oui.*regexp.RegexpDans l'exemple, la régularité eststringProduits)
- 7. SubexpNames() Méthodes(Renvoie le nom du Groupe régulier)
1. QuoteMeta() Fonctions(Conversion spéciale de caractères)
Convertir les caractères spéciaux d'une expression régulière.
Syntaxe
func QuoteMeta(s string) string
Exemple complet
package main
import (
"fmt"
"regexp"
)
func main() {
pattern := "Hyund..*?"
myString := regexp.QuoteMeta(pattern)
fmt.Println(myString)
}
- Résultats
Hyund.\.\*\?
2. LiteralPrefix()Méthodes(Trouver un préfixe commun régulier)
Syntaxe
func (re *Regexp) LiteralPrefix() (prefix string, complete bool)
prefix: Préfixe commun
complete:Si prefix C'est l'expression régulière elle - même ,Renvoie true,Sinon, retournez à false
Exemple complet
- 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())
}
- Résultats
Hello false
Hello true
false
Comme ci - dessus
- Résultat 1,Préfixe commun
helloRetour,falseIndique que le préfixe n'est pas la chaîne d'expression régulière elle - même- Résultat 2,Préfixe commun
helloRetour,trueIndique que le préfixe est la chaîne d'expression régulière elle - même- Résultat 3, Pas de préfixe commun
3. Longest()Méthodes(Basculer le mode avide)
Syntaxe
func (re *Regexp) Longest()
Exemple complet
package main
import (
"fmt"
"regexp"
)
func main() {
s := `10.10.239.11`
reg := regexp.MustCompile(".*?\\.")
fmt.Printf("%s\n", reg.FindString(s))
reg.Longest() // Basculer le mode avide
fmt.Printf("%s\n", reg.FindString(s))
}
- Résultats
10.
10.10.239.
4. NumSubexp()Méthodes(Nombre de groupes dans la requête régulière)
Syntaxe
func (re *Regexp) NumSubexp() int
Exemple complet
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("(\\d+)\\.(\\d+)")
n := reg.NumSubexp()
fmt.Printf(" Le nombre de groupes dans le regular est :%d",n)
}
- Résultats
Le nombre de groupes dans le regular est :2
5. Split() Méthodes(Coupe régulière)
Syntaxe
func (re *Regexp) Split(s string, n int) []string
Exemple complet
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile(`\.`)
myString := "www.xishu.com"
result := reg.Split(myString, -1)
fmt.Printf("Résultats:%q\n",result )
}
- Résultats
Résultats:["www" "xishu" "com"]
6. String()Méthodes (Oui.*regexp.RegexpDans l'exemple, la régularité eststringProduits)
Syntaxe
func (re *Regexp) String() string
Exemple complet
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\d+$")
pattern := reg.String()
fmt.Printf("%s\n", pattern)
}
- Résultats
\d+$
7. SubexpNames() Méthodes(Renvoie le nom du Groupe régulier)
Syntaxe
func (re *Regexp) SubexpNames() []string
Nommer les groupes réguliers :
(?P<NAME>)
Exemple complet
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)
}
- Résultats
[ first second third fourth]
https://blog.csdn.net/qq_41629756/article/details/100516635
边栏推荐
- Interpreting Oracle
- UE5全局光照系统Lumen解析与优化
- Golang--channel and select
- Please advise tonghuashun which securities firm to choose for opening an account? Is it safe to open an account online?
- 音视频与CPU架构
- Classic quotations from "human nature you must not know"
- Gold three silver four~
- Camtasia 2022 new ultra clear recording computer video
- Dreamcamera2 video recording, playing without sound, recording function is normal, using a third-party application for video recording, playing with sound
- Lumen Analysis and Optimization of ue5 global Lighting System
猜你喜欢

Camtasia 2022 new ultra clear recording computer video

Oracle connectivity issues and Solutions

Add console programs in UE

附加:HikariCP连接池简述;(并没有深究,只是对HikariCP连接池有个基本认识)

学习太极创客 — MQTT(四)服务端连接操作

组件与路由

《你不可不知的人性》經典語錄

DF reports an error stale file handle

Redis configuration and optimization of NoSQL

The difference between like,%, - in database query SQL
随机推荐
R language Markov chain Monte Carlo: practical introduction
How to default that an app is not restricted by traffic
附加:HikariCP连接池简述;(并没有深究,只是对HikariCP连接池有个基本认识)
Add console programs in UE
Translation notes of orb-slam series papers
ORB-SLAM3论文概述
我在中山,到哪里开户比较好?网上开户是否安全么?
High availability in Internet three highs (high concurrency, high performance and high availability)
Version management tool usage
Survival analysis based on ovarian data set
Mongoose - Why we make “mongoose.Promise = global.Promise” when setting a mongoose module?
分布式电商项目 谷粒商城 学习笔记<3>
Gd32 ADC acquisition voltage
Remember a simple JVM tuning experience
Deep understanding of distributed cache design
程序员的八年工资变动,令网友羡慕不已:你一个月顶我一年工资
Fresh graduates talk about their graduation stories
Lumen Analysis and Optimization of ue5 global Lighting System
Can the main RF circuit be removed for projects that do not need the main RF?
解读Oracle