当前位置:网站首页>Precautions and skills in using regular expressions in golang
Precautions and skills in using regular expressions in golang
2022-07-01 08:05:00 【dorlolo】
golang Precautions and techniques for using regular expressions in
regexp modular
View help documents :
Command line view :go doc regexp/syntax
Check online :
- Chinese document :
Novice tutorial Some regular methods are introduced in detail
GO Language Chinese document Explain some difficult basic grammar , Easier to understand than official documents - English document :
https://pkg.go.dev/regexp go Standard library documents , There are many examples .
About the use of escape characters
Use similar in backquotes \w(\w representative 0-9A-Za-z_) This syntax does not need to do any processing
grep, _ := regexp.Compile(`\w{1,}`)
And in double quotation marks , Backslash represents escape character . So use \w when , Need to use an extra \ Escape backslash to normal string , Otherwise, the report will be wrong
grep, _ := regexp.Compile("\\w{1,}")
About most and least matches
python in , The minimum match is a function that can be called . and go Of regexp Module has no minimum matching function .
If you want to use minimum matching , Regular syntax is required , It is introduced here in the document
repeat :
x* repeat >=0 Secondary match x, The more the better ( Priority repeat matching x)
x+ repeat >=1 Secondary match x, The more the better ( Priority repeat matching x)
x? 0 or 1 Secondary match x, first 1 Time
x{n,m} n To m Secondary match x, The more the better ( Priority repeat matching x)
x{n,} repeat >=n Secondary match x, The more the better ( Priority repeat matching x)
x{n} repeat n Secondary match x
x*? repeat >=0 Secondary match x, The less, the better. ( Jump out of repetition first )
x+? repeat >=1 Secondary match x, The less, the better. ( Jump out of repetition first )
x?? 0 or 1 Secondary match x, first 0 Time
x{n,m}? n To m Secondary match x, The less, the better. ( Jump out of repetition first )
x{n,}? repeat >=n Secondary match x, The less, the better. ( Jump out of repetition first )
x{n}? repeat n Secondary match x
In the explanation above , With less, the better is the minimum match .* and + If you are not careful, you will match the full text
str := `" The tiger king honors Superman "` // String to find
re := regexp.MustCompile("[\u4e00-\u9fa5]{1,3}") // Matching rules , matching 1 To 3 Chinese characters
fmt.Println(re.FindAllString(str, -1)) // Inquire about
Output
[ Tiger Big Wang Zerong Yao Superman ]
About .*? The pit of
stay javaScript and python In other languages ,.*? Represents a query All arbitrary strings .
And in the golang in , This is stated in the document
x*? repeat >=0 Secondary match x, The less, the better. ( Jump out of repetition first )
Be careful , The point is that the matching principle is content The less, the better. , And regular expressions .*? Medium . Represents any string ( Include empty string ).
According to this matching principle , Empty strings are always matched first , This leads to the following problems :
func TestFlag5(t *testing.T) {
data := " Have you eaten yet? "
grep, _ := regexp.Compile(".*?")
result := grep.FindAllString(data, -1)
t.Log(result)
}
The output is
structToJson_test.go:107: [ ]
It matches only five empty strings .
There are two solutions . The first one is , Remove the question mark , become .* that will do . To view the document ,(.* The principle of matching is that the more the better )
func TestFlag6(t *testing.T) {
data1 := " Have you eaten yet? "
grep, _ := regexp.Compile(".*")//.*? Changed to .*
result := grep.FindAllString(data1, -1)
t.Log(result)
}
Output
structToJson_test.go:107: [ Have you eaten yet? ]
Second option ,.*? Must be followed by a string
func TestFlag7(t *testing.T) {
str := ` Have you eaten yet? ` // String to find
re, _ := regexp.Compile(".*? Did you? ") //.*? Followed by a few strings , It doesn't matter whether you add it at the beginning
t.Log(re.FindAllString(str, -1)) // Inquire about
}
Output
[ Have you eaten yet? ]
About matching Chinese characters
There is a pit here to match Chinese characters , If the matching rule uses double quotation marks , Enter the matching rule normally
re := regexp.MustCompile("[\u4e00-\u9fa5]{1,}")// Matching rules ,\u4e00-\u9fa5 Express unicoce People in a Chinese character
But if you use backquotes to wrap the above matching rules , Query will result in panic Report errors
re := regexp.MustCompile(`[\u4e00-\u9fa5]{1,}`)// The wrong sample
You need to write a matching rule wrapped in double quotation marks , Then assign and paste in , Finally, it turns into a string of random codes , That's right .
re := regexp.MustCompile(`[ One - A kind of ]{1,}`) // Matching rules
Description of basic query function
Common query methods
FindAllString: Complete match , Query only once , The query returns ;FindString: Complete match , You can specify the number of queries ( All or more );FindStringSubmatch: Subitem query , Get the content specified in the query criteria . Query only once , The query returns ;FindAllStringSubmatch: Subitem query , Get the content specified in the query criteria . You can specify the number of queries ( All or more ).
Subitem query FindStringSubmatch
Be careful :
- Use English brackets to wrap the contents to be obtained in the required regular
- Use here
FindStringSubmatchMethod to search . Query only once .
func TestFlag5(t *testing.T) {
str := `_A_123567abv` // String to find
re, _ := regexp.Compile("_\\w_(.*)") // Matching rules
fmt.Println(re.FindStringSubmatch(str)) // Inquire about
}
Output :
[_A_123567abv 123567abv]
In search results , The first is the exact match in the regular . The second start is the child you want to get .
Subitem query FindAllStringSubmatch
In group query , In regular expressions , Use the content to be queried () Cover up .
// Query content
data1 := "type Demo struct {
Name string `json:\"name\" form:\"name\" gorm:\"column:name;comment: \"`
Age int `json:\"age\" form:\"age\" gorm:\"column:age;comment: \"`
}"
// Regular expressions : Query structure fields 、 Field type and json Content in Tags
grep, _ := regexp.Compile(`([\w]{1,})[ ]{1,}([\[\]\w]{2,})[ ]{1,}` + "`" + `(json):"([\w]{1,})[,]{0,}(\w{0,})\"` + ".*`")
// Use FindAllStringSubmatch Function query
result := grep.FindAllStringSubmatch(*s.StructValue, -1)
Output results :
[
[Name string `json:"name" form:"name" gorm:"column:name;comment: "` Name string json name ] [Age int `json:"age" form:"age" gorm:"column:age;comment: "` Age int json age ]
]
Be careful , ad locum , Each matching search result is placed in a separate slice .
边栏推荐
- Programmer's regimen
- 【无标题】
- 力扣——求一组字符中的第一个回文字符
- 【入门】提取不重复的整数
- How to make the two financial transactions faster
- Gui Gui programming (XV) - use scale to control font size changes
- QT -- 1. QT connection database
- 2022 mobile crane driver test exercises and online simulation test
- AArdio - 【问题】bass库回调时内存增长的问题
- Latex formula code
猜你喜欢

Lm08 mesh series mesh inversion (fine)

Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?

Access report realizes subtotal function

【入门】输入n个整数,输出其中最小的k个

Principle and process of embossing

Android screen adaptation (using constraintlayout), kotlin array sorting

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

Cyclic neural network

Access报表实现小计功能

Differential: definition of total differential, partial derivative, gradient
随机推荐
【mysql学习笔记27】存储过程
getInputStream() has already been called for this request
事务方法调用@Transactional
How relational databases work
Introduction to kubernetes resource objects and common commands (II)
Two expressions of string
base64
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
LSTM of RNN
Scala language learning-07-constructor
【刷题】字符统计【0】
SQL number injection and character injection
Find the nearest n-th power of 2
ContentType所有类型对比
【mysql学习笔记26】视图
Caesar
038 network security JS
Conscience Amway universal wheel SolidWorks model material website
php laravel微信支付
Missing API interface actual development series (14): ID card real name authentication verification