Go 語言入門學(xué)習(xí)之正則表達(dá)式
前言
在計(jì)算中,我們經(jīng)常需要將特定模式的字符或字符子集匹配為另一個(gè)字符串中的字符串。此技術(shù)用于使用特別的語法來搜索給定字符串中的特定字符集。
如果搜索到的模式匹配,或者在目標(biāo)字符串中找到給定的子集,則搜索被稱為成功;否則被認(rèn)為是不成功的。
什么是正則表達(dá)式
正則表達(dá)式(或 RegEx)是一個(gè)特殊的字符序列,它定義了用于匹配特定文本的搜索模式。在 Golang 中,有一個(gè)內(nèi)置的正則表達(dá)式包: ??regexp?? 包,其中包含所有操作列表,如過濾、修改、替換、驗(yàn)證或提取。
正則表達(dá)式可以用于文本搜索和更高級(jí)的文本操作。正則表達(dá)式內(nèi)置于 grep 和 sed 等工具,vi 和 emacs 等文本編輯器,Go、Java 和 Python 等編程語言中。表達(dá)式的語法主要遵循這些流行語言中使用的已建立的 RE2 語法。 RE2 語法是 PCRE 的一個(gè)子集,有各種注意事項(xiàng)。
MatchString 函數(shù)
??MatchString()?? 函數(shù)報(bào)告作為參數(shù)傳遞的字符串是否包含正則表達(dá)式模式的任何匹配項(xiàng)。
package main
import (
"fmt"
"log"
"regexp"
)
func main() {
words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}
for _, word := range words {
found, err := regexp.MatchString(".even", word)
if err != nil {
log.Fatal(err)
}
if found {
fmt.Printf("%s matches\n", word)
} else {
fmt.Printf("%s does not match\n", word)
}
}
}運(yùn)行該代碼:
Seven matches
even does not match
Maven does not match
Amen does not match
eleven matches
但同時(shí)我們能看到編輯器有提示:

編譯器已經(jīng)開始提醒我們,??MatchString?? 直接使用性能很差,所以考慮使用 ??regexp.Compile?? 函數(shù)。
Compile 函數(shù)
??Compile?? 函數(shù)解析正則表達(dá)式,如果成功,則返回可用于匹配文本的 Regexp 對象。編譯的正則表達(dá)式產(chǎn)生更快的代碼。
??MustCompile?? 函數(shù)是一個(gè)便利函數(shù),它編譯正則表達(dá)式并在無法解析表達(dá)式時(shí)發(fā)生 panic。
package main
import (
"fmt"
"log"
"regexp"
)
func main() {
words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}
re, err := regexp.Compile(".even")
if err != nil {
log.Fatal(err)
}
for _, word := range words {
found := re.MatchString(word)
if found {
fmt.Printf("%s matches\n", word)
} else {
fmt.Printf("%s does not match\n", word)
}
}
}在代碼示例中,我們使用了編譯的正則表達(dá)式。
re, err := regexp.Compile(".even")即使用 ??Compile?? 編譯正則表達(dá)式。然后在返回的正則表達(dá)式對象上調(diào)用 ??MatchString?? 函數(shù):
found := re.MatchString(word)
運(yùn)行程序,能看到同樣的代碼:
Seven matches
even does not match
Maven does not match
Amen does not match
eleven matches
MustCompile 函數(shù)
package main
import (
"fmt"
"regexp"
)
func main() {
words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}
re := regexp.MustCompile(".even")
for _, word := range words {
found := re.MatchString(word)
if found {
fmt.Printf("%s matches\n", word)
} else {
fmt.Printf("%s does not match\n", word)
}
}
}FindAllString 函數(shù)
??FindAllString?? 函數(shù)返回正則表達(dá)式的所有連續(xù)匹配的切片。
package main
import (
"fmt"
"os"
"regexp"
)
func main() {
var content = `Foxes are omnivorous mammals belonging to several genera
of the family Canidae. Foxes have a flattened skull, upright triangular ears,
a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every
continent except Antarctica. By far the most common and widespread species of
fox is the red fox.`
re := regexp.MustCompile("(?i)fox(es)?")
found := re.FindAllString(content, -1)
fmt.Printf("%q\n", found)
if found == nil {
fmt.Printf("no match found\n")
os.Exit(1)
}
for _, word := range found {
fmt.Printf("%s\n", word)
}
}在代碼示例中,我們找到了單詞 fox 的所有出現(xiàn),包括它的復(fù)數(shù)形式。
re := regexp.MustCompile("(?i)fox(es)?")使用 (?i) 語法,正則表達(dá)式不區(qū)分大小寫。 (es)?表示“es”字符可能包含零次或一次。
found := re.FindAllString(content, -1)
我們使用 ??FindAllString?? 查找所有出現(xiàn)的已定義正則表達(dá)式。第二個(gè)參數(shù)是要查找的最大匹配項(xiàng); -1 表示搜索所有可能的匹配項(xiàng)。
運(yùn)行結(jié)果:
["Foxes" "Foxes" "Foxes" "fox" "fox"]
Foxes
Foxes
Foxes
fox
fox
FindAllStringIndex 函數(shù)
package main
import (
"fmt"
"regexp"
)
func main() {
var content = `Foxes are omnivorous mammals belonging to several genera
of the family Canidae. Foxes have a flattened skull, upright triangular ears,
a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every
continent except Antarctica. By far the most common and widespread species of
fox is the red fox.`
re := regexp.MustCompile("(?i)fox(es)?")
idx := re.FindAllStringIndex(content, -1)
for _, j := range idx {
match := content[j[0]:j[1]]
fmt.Printf("%s at %d:%d\n", match, j[0], j[1])
}
}在代碼示例中,我們在文本中找到所有出現(xiàn)的 fox 單詞及其索引。
Foxes at 0:5
Foxes at 81:86
Foxes at 196:201
fox at 296:299
fox at 311:314
Split 函數(shù)
??Split?? 函數(shù)將字符串切割成由定義的正則表達(dá)式分隔的子字符串。它返回這些表達(dá)式匹配之間的子字符串切片。
package main
import (
"fmt"
"log"
"regexp"
"strconv"
)
func main() {
var data = `22, 1, 3, 4, 5, 17, 4, 3, 21, 4, 5, 1, 48, 9, 42`
sum := 0
re := regexp.MustCompile(",\s*")
vals := re.Split(data, -1)
for _, val := range vals {
n, err := strconv.Atoi(val)
sum += n
if err != nil {
log.Fatal(err)
}
}
fmt.Println(sum)
}在代碼示例中,我們有一個(gè)逗號(hào)分隔的值列表。我們從字符串中截取值并計(jì)算它們的總和。
re := regexp.MustCompile(",\s*")正則表達(dá)式包括一個(gè)逗號(hào)字符和任意數(shù)量的相鄰空格。
vals := re.Split(data, -1)
我們得到了值的一部分。
for _, val := range vals {
n, err := strconv.Atoi(val)
sum += n
if err != nil {
log.Fatal(err)
}
}我們遍歷切片并計(jì)算總和。切片包含字符串;因此,我們使用 ??strconv.Atoi?? 函數(shù)將每個(gè)字符串轉(zhuǎn)換為整數(shù)。
運(yùn)行代碼:
189
Go 正則表達(dá)式捕獲組
圓括號(hào) () 用于創(chuàng)建捕獲組。這允許我們將量詞應(yīng)用于整個(gè)組或?qū)⒔惶嫦拗茷檎齽t表達(dá)式的一部分。為了找到捕獲組(Go 使用術(shù)語子表達(dá)式),我們使用 ??FindStringSubmatch?? 函數(shù)。
package main
import (
"fmt"
"regexp"
)
func main() {
websites := [...]string{"webcode.me", "zetcode.com", "freebsd.org", "netbsd.org"}
re := regexp.MustCompile("(\w+)\.(\w+)")
for _, website := range websites {
parts := re.FindStringSubmatch(website)
for i, _ := range parts {
fmt.Println(parts[i])
}
fmt.Println("---------------------")
}
}在代碼示例中,我們使用組將域名分為兩部分。
re := regexp.MustCompile("(\w+)\.(\w+)")我們用括號(hào)定義了兩個(gè)組。
parts := re.FindStringSubmatch(website)
??FindStringSubmatch?? 返回包含匹配項(xiàng)的字符串切片,包括來自捕獲組的字符串。
運(yùn)行代碼:
$ go run capturegroups.go
webcode.me
webcode
me
---------------------
zetcode.com
zetcode
com
---------------------
freebsd.org
freebsd
org
---------------------
netbsd.org
netbsd
org
---------------------
正則表達(dá)式替換字符串
可以用 ??ReplaceAllString?? 替換字符串。該方法返回修改后的字符串。
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
)
func main() {
resp, err := http.Get("http://webcode.me")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
content := string(body)
re := regexp.MustCompile("<[^>]*>")
replaced := re.ReplaceAllString(content, "")
fmt.Println(strings.TrimSpace(replaced))
}該示例讀取網(wǎng)頁的 HTML 數(shù)據(jù)并使用正則表達(dá)式去除其 HTML 標(biāo)記。
resp, err := http.Get("http://webcode.me")我們使用 http 包中的 Get 函數(shù)創(chuàng)建一個(gè) GET 請求。
body, err := ioutil.ReadAll(resp.Body)
我們讀取響應(yīng)對象的主體。
re := regexp.MustCompile("<[^>]*>")這個(gè)模式定義了一個(gè)匹配 HTML 標(biāo)簽的正則表達(dá)式。
replaced := re.ReplaceAllString(content, "")
我們使用 ReplaceAllString 方法刪除所有標(biāo)簽。
ReplaceAllStringFunc 函數(shù)
??ReplaceAllStringFunc?? 返回一個(gè)字符串的副本,其中正則表達(dá)式的所有匹配項(xiàng)都已替換為指定函數(shù)的返回值。
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
content := "an old eagle"
re := regexp.MustCompile(`[^aeiou]`)
fmt.Println(re.ReplaceAllStringFunc(content, strings.ToUpper))
}在代碼示例中,我們將 ??strings.ToUpper?? 函數(shù)應(yīng)用于字符串的所有字符。
$ go run replaceallfunc.go aN oLD eaGLe
總結(jié)
模式匹配在根據(jù)基于正則表達(dá)式和語法的特定搜索模式在字符串中搜索某些字符集時(shí)起著重要作用。
匹配的模式允許我們從字符串中提取所需的數(shù)據(jù)并以我們喜歡的方式對其進(jìn)行操作。理解和使用正則表達(dá)式是處理文本的關(guān)鍵。
在實(shí)踐中,程序員會(huì)保留一組常用的正則表達(dá)式來匹配電子郵件、電話號(hào)碼等,并在需要時(shí)使用和重用它。
到此這篇關(guān)于Go 語言入門學(xué)習(xí)之正則表達(dá)式的文章就介紹到這了,更多相關(guān)Go正則表達(dá)式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Golang棧結(jié)構(gòu)和后綴表達(dá)式實(shí)現(xiàn)計(jì)算器示例
- 一文帶你全面掌握Go語言中的正則表達(dá)式
- Go語句與表達(dá)式案例手冊深度解析
- 在?Go?語言中使用?regexp?包處理正則表達(dá)式的操作
- Go語言實(shí)戰(zhàn)之詳細(xì)掌握正則表達(dá)式的應(yīng)用與技巧
- Golang中正則表達(dá)式語法及相關(guān)示例
- Go語言利用正則表達(dá)式處理多行文本
- Go中regexp包常見的正則表達(dá)式操作
- Go正則表達(dá)式匹配字符串,替換字符串方式
- Go語言結(jié)合正則表達(dá)式實(shí)現(xiàn)高效獲取數(shù)據(jù)
- Go expr 通用表達(dá)式引擎的使用
相關(guān)文章
使用golang實(shí)現(xiàn)一個(gè)MapReduce的示例代碼
這篇文章主要給大家介紹了關(guān)于如何使用golang實(shí)現(xiàn)一個(gè)MapReduce,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
詳解Go語言RESTful JSON API創(chuàng)建
這篇文章主要介紹了詳解Go語言RESTful JSON API創(chuàng)建,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
golang實(shí)現(xiàn)整型和字節(jié)數(shù)組之間的轉(zhuǎn)換操作
這篇文章主要介紹了golang實(shí)現(xiàn)整型和字節(jié)數(shù)組之間的轉(zhuǎn)換操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Go語言CSP并發(fā)模型goroutine及channel底層實(shí)現(xiàn)原理
這篇文章主要為大家介紹了Go語言CSP并發(fā)模型goroutine?channel底層實(shí)現(xiàn)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Golang?內(nèi)存模型The?Go?Memory?Model
這篇文章主要為大家介紹了Golang?內(nèi)存模型The?Go?Memory?Model實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Go結(jié)構(gòu)體從基礎(chǔ)到應(yīng)用深度探索
本文深入探討了結(jié)構(gòu)體的定義、類型、字面量表示和使用方法,旨在為讀者呈現(xiàn)Go結(jié)構(gòu)體的全面視角,通過結(jié)構(gòu)體,開發(fā)者可以實(shí)現(xiàn)更加模塊化、高效的代碼設(shè)計(jì),這篇文章旨在為您提供關(guān)于結(jié)構(gòu)體的深入理解,助您更好地利用Go語言的強(qiáng)大功能2023-10-10
詳解Go語言如何實(shí)現(xiàn)并發(fā)安全的map
go語言提供的數(shù)據(jù)類型中,只有channel是并發(fā)安全的,基礎(chǔ)map并不是并發(fā)安全的,本文為大家整理了三種實(shí)現(xiàn)了并發(fā)安全的map的方案,有需要的可以參考下2023-12-12
GO語言中defer實(shí)現(xiàn)原理的示例詳解
這篇文章主要為大家詳細(xì)介紹了Go語言中defer實(shí)現(xiàn)原理的相關(guān)資料,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Go語言有一定的幫助,需要的可以參考一下2023-02-02

