当前位置:网站首页>Go language foundation ----- 11 ----- regular expression
Go language foundation ----- 11 ----- regular expression
2022-07-03 07:37:00 【Mango sauce】
1 Regular expression Introduction
Regular expression is a complex and powerful tool for pattern matching and text manipulation . Although regular expressions are less efficient than pure text matching , But it's more flexible . According to its grammatical rules , The matching pattern constructed on demand can filter almost any character combination you want from the original text .
Go Language passing regexp The standard package provides official support for regular expressions , If you've used the regular features of other programming languages , So you should be right Go The language version is not too strange , But there are also some small differences between them , because Go What we achieve is RE2 standard , except \C, Detailed syntax description reference :http://code.google.com/p/re2/wiki/Syntax.
In fact, string processing we can use strings Package to search (Contains、Index)、 Replace (Replace) And analysis (Split、Join) Wait for the operation , But these are simple string operations , Their search is case sensitive , And fixed strings , If we need to match the variable one, we can't do it , Of course, if strings Bag can solve your problem , Then try to use it to solve . Because they're simple enough 、 And the performance and readability will be better than regular .
2 Regular matching rule graph
Refer to the official website : https://studygolang.com/pkgdoc.
2.1 Example 1: Order number (.) Match any type .
package main
import (
"fmt"
"regexp"
)
func main(){
buf := "abc azc a7c aac 888 a9c tac"
//1) Interpretation rules , It parses regular expressions , If successful, return to the interpreter
reg1 := regexp.MustCompile(`a.c`)
if reg1 == nil {
fmt.Println("regexp err")
return
}
//2) Extract key information according to rules
result1 := reg1.FindAllStringSubmatch(buf, -1)
fmt.Println("result1 = ", result1)
}

2.2 Example 2: matching a[0-9]c Value between .
package main
import (
"fmt"
"regexp"
)
func main(){
buf := "abc azc a7c aac 888 a9c tac"
//1) Interpretation rules , It parses regular expressions , If successful, return to the interpreter
reg1 := regexp.MustCompile(`a[0-9]c`)
if reg1 == nil {
// Explanation failed , return nil
fmt.Println("regexp err")
return
}
//2) Extract key information according to rules
result1 := reg1.FindAllStringSubmatch(buf, -1)
fmt.Println("result1 = ", result1)
}

2.3 Example 3:\d matching a[0-9]c Value between .
package main
import (
"fmt"
"regexp"
)
func main(){
buf := "abc azc a7c aac 888 a9c tac"
//1) Interpretation rules , It parses regular expressions , If successful, return to the interpreter
reg1 := regexp.MustCompile(`a\dc`)
if reg1 == nil {
// Explanation failed , return nil
fmt.Println("regexp err")
return
}
//2) Extract key information according to rules
result1 := reg1.FindAllStringSubmatch(buf, -1)
fmt.Println("result1 = ", result1)
}
Results and examples 2 equally .

2.4 Example 4:+ Matches the previous character 1 Times or times .
package main
import (
"fmt"
"regexp"
)
func main(){
buf := "43.14 567 agsdg 1.23 7. 8.9 1sdljgl 6.66 7.8 "
// Explain regular expressions , + Matches the previous character 1 Times or times
reg := regexp.MustCompile(`\d+\.\d+`)
if reg == nil {
fmt.Println("MustCompile err")
return
}
// Extract key information
//result := reg.FindAllString(buf, -1)
result := reg.FindAllStringSubmatch(buf, -1)
fmt.Println("result = ", result)
}

2.5 Example 5: Filter labeled or unlabeled .
package main
import (
"fmt"
"regexp"
)
func main(){
//`` Native string
buf := ` <!DOCTYPE html> <html lang="zh-CN"> <head> <title>Go Language standard library document Chinese version | Go Chinese language network | Golang The Chinese community | Golang China </title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"> <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1"> <meta charset="utf-8"> <link rel="shortcut icon" href="/static/img/go.ico"> <link rel="apple-touch-icon" type="image/png" href="/static/img/logo2.png"> <meta name="author" content="polaris <[email protected]>"> <meta name="keywords" content=" chinese , file , Standard library , Go Language ,Golang,Go Community ,Go The Chinese community ,Golang The Chinese community ,Go Language community ,Go Language learning , Study Go Language ,Go Language learning garden ,Golang China ,Golang China ,Golang China, Go Language Forum , Go Chinese language network "> <meta name="description" content="Go Language document Chinese version ,Go Chinese language network , China Golang Community ,Go Language learning garden , Committed to building a perfect Golang The Chinese community ,Go Learning home for language lovers . Share Go language knowledge , Exchange experience "> </head> <div> And hobbies </div> <div> ha-ha Are you there be not in </div> <div> test </div> <div> Come here </div> <frameset cols="15,85"> <frame src="/static/pkgdoc/i.html"> <frame name="main" src="/static/pkgdoc/main.html" tppabs="main.html" > <noframes> </noframes> </frameset> </html> `
// Explain regular expressions , + Matches the previous character 1 Times or times
//reg := regexp.MustCompile(`<div>(.*)</div>`)
//.* Indicates that all current contents are matched . At this point, the current is <div></div> The content of both .* Followed by ? after , namely *? Indicates matching duplicate items , That is, if there is a repetition, it will only be displayed once , The less, the better. .
//?s:: Indicates that line breaks are also matched .
reg := regexp.MustCompile(`<div>(?s:(.*?))</div>`)
if reg == nil {
fmt.Println("MustCompile err")
return
}
// Extract key information
result := reg.FindAllStringSubmatch(buf, -1)
//fmt.Println("result = ", result)
// Filter <></>
for _, text := range result {
// Filter tagged
//fmt.Println("text[0] = ", text[0]) // belt <></>
// Filter unlabeled
fmt.Println("text[1] = ", text[1]) // No <></>
}
}

边栏推荐
- 技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
- c语言指针的概念
- PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
- PAT甲级 1028 List Sorting
- 论文学习——鄱阳湖星子站水位时间序列相似度研究
- Circuit, packet and message exchange
- An overview of IfM Engage
- 哪一刻你才发现青春结束了
- Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
- GStreamer ffmpeg avdec decoded data flow analysis
猜你喜欢
随机推荐
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
Comparison of advantages and disadvantages between most complete SQL and NoSQL
圖像識別與檢測--筆記
Chapter VI - Containers
Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
Jeecg data button permission settings
昇思MindSpore再升级,深度科学计算的极致创新
Talk about floating
技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务
【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
TreeMap
VMWare网络模式-桥接,Host-Only,NAT网络
Vertx restful style web router
Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer
Inverted chain disk storage in Lucene (pfordelta)
List exercises after class
Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
Beginners use Minio
VMware network mode - bridge, host only, NAT network
The concept of C language pointer









