Go中regexp包常見的正則表達(dá)式操作
在 Golang 中,regexp
包用于處理正則表達(dá)式操作。以下是一些常見的正則表達(dá)式操作的代碼示例:
1. 簡(jiǎn)單匹配(MatchString)
用于檢查字符串是否匹配某個(gè)正則表達(dá)式。
package main import ( "fmt" "regexp" ) func main() { pattern := `^hello` text := "hello world" match, _ := regexp.MatchString(pattern, text) fmt.Println("Matched:", match) // 輸出: Matched: true }
2. 編譯正則表達(dá)式(Compile 和 MustCompile)
通過 regexp.Compile
或 regexp.MustCompile
編譯正則表達(dá)式以提高性能。
package main import ( "fmt" "regexp" ) func main() { // Compile 返回 error,如果正則無效 re, err := regexp.Compile(`\d+`) if err != nil { fmt.Println("Error compiling regex:", err) return } text := "Order number 12345" fmt.Println("Matched:", re.MatchString(text)) // 輸出: Matched: true // MustCompile 會(huì) panic,如果正則無效 re2 := regexp.MustCompile(`\d+`) fmt.Println("Matched:", re2.MatchString(text)) // 輸出: Matched: true }
3. 查找字符串中的第一個(gè)匹配項(xiàng)(FindString 和 FindStringSubmatch)
FindString
返回第一個(gè)匹配的字符串。FindStringSubmatch
返回第一個(gè)匹配的字符串以及捕獲的子組。
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`(\d+)-(\d+)-(\d+)`) text := "Today's date is 2025-01-25." // 找到第一個(gè)匹配的字符串 match := re.FindString(text) fmt.Println("Found:", match) // 輸出: Found: 2025-01-25 // 找到第一個(gè)匹配及其子組 submatches := re.FindStringSubmatch(text) fmt.Println("Submatches:", submatches) // 輸出: Submatches: [2025-01-25 2025 01 25] }
4. 查找所有匹配項(xiàng)(FindAllString 和 FindAllStringSubmatch)
FindAllString
返回所有匹配的字符串。FindAllStringSubmatch
返回所有匹配的字符串及其子組。
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\d+`) text := "Numbers: 123, 456, and 789." // 找到所有匹配的字符串 matches := re.FindAllString(text, -1) fmt.Println("Matches:", matches) // 輸出: Matches: [123 456 789] // 限制返回的匹配數(shù)量 limitedMatches := re.FindAllString(text, 2) fmt.Println("Limited Matches:", limitedMatches) // 輸出: Limited Matches: [123 456] }
5. 替換字符串(ReplaceAllString)
用于替換所有匹配的字符串。
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\d+`) text := "Order 123, 456, and 789." // 替換所有匹配的數(shù)字為 XXX result := re.ReplaceAllString(text, "XXX") fmt.Println("Replaced:", result) // 輸出: Replaced: Order XXX, XXX, and XXX. }
6. 替換字符串(ReplaceAllStringFunc)
通過一個(gè)函數(shù)動(dòng)態(tài)替換匹配的字符串。
package main import ( "fmt" "regexp" "strings" ) func main() { re := regexp.MustCompile(`[a-z]+`) text := "hello world GO!" // 將匹配的字符串替換為大寫 result := re.ReplaceAllStringFunc(text, strings.ToUpper) fmt.Println("Replaced:", result) // 輸出: Replaced: HELLO WORLD GO! }
7. 分割字符串(Split)
使用正則表達(dá)式分割字符串。
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\s+`) // 匹配空白字符 text := "Split this string!" // 分割字符串 parts := re.Split(text, -1) fmt.Println("Parts:", parts) // 輸出: Parts: [Split this string!] }
8. 提取子組并命名(Named Captures)
通過命名捕獲組提取特定的子組。
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`(?P<Year>\d{4})-(?P<Month>\d{2})-(?P<Day>\d{2})`) text := "Date: 2025-01-25." // 提取所有子組 submatches := re.FindStringSubmatch(text) fmt.Println("Submatches:", submatches) // 輸出: Submatches: [2025-01-25 2025 01 25] // 提取命名的子組 names := re.SubexpNames() for i, name := range names { if name != "" { fmt.Printf("%s: %s\n", name, submatches[i]) } } // 輸出: // Year: 2025 // Month: 01 // Day: 25 }
9. 檢查字符串起始位置匹配(MatchString 和 Match)
MatchString
檢查整個(gè)字符串。Match
檢查字節(jié)切片。
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`^hello`) text := "hello world" bytes := []byte("hello bytes") fmt.Println("String Match:", re.MatchString(text)) // 輸出: String Match: true fmt.Println("Bytes Match:", re.Match(bytes)) // 輸出: Bytes Match: true }
10. 替換字節(jié)切片(ReplaceAll)
與字符串操作類似,但作用于字節(jié)切片。
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\d+`) text := []byte("Order 123, 456, and 789.") // 替換所有匹配的數(shù)字為 XXX result := re.ReplaceAll(text, []byte("XXX")) fmt.Println("Replaced:", string(result)) // 輸出: Replaced: Order XXX, XXX, and XXX. }
到此這篇關(guān)于Go中regexp包常見的正則表達(dá)式操作的文章就介紹到這了,更多相關(guān)Go regexp正則表達(dá)式操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Go 語言入門學(xué)習(xí)之正則表達(dá)式
- Golang棧結(jié)構(gòu)和后綴表達(dá)式實(shí)現(xiàn)計(jì)算器示例
- 一文帶你全面掌握Go語言中的正則表達(dá)式
- Go語句與表達(dá)式案例手冊(cè)深度解析
- 在?Go?語言中使用?regexp?包處理正則表達(dá)式的操作
- Go語言實(shí)戰(zhàn)之詳細(xì)掌握正則表達(dá)式的應(yīng)用與技巧
- Golang中正則表達(dá)式語法及相關(guān)示例
- Go語言利用正則表達(dá)式處理多行文本
- Go正則表達(dá)式匹配字符串,替換字符串方式
- Go語言結(jié)合正則表達(dá)式實(shí)現(xiàn)高效獲取數(shù)據(jù)
- Go expr 通用表達(dá)式引擎的使用
相關(guān)文章
Go語言中利用http發(fā)起Get和Post請(qǐng)求的方法示例
這篇文章主要給大家介紹了關(guān)于Go語言中利用http發(fā)起Get和Post請(qǐng)求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11Go標(biāo)準(zhǔn)庫http與fasthttp服務(wù)端性能對(duì)比場(chǎng)景分析
這篇文章主要介紹了Go標(biāo)準(zhǔn)庫http與fasthttp服務(wù)端性能比較,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

Golang+Vue輕松構(gòu)建Web應(yīng)用的方法步驟