Go語言標準庫之regexp

2021-10-23 16:09:48 字數 1231 閱讀 7074

regexp是go支援正規表示式的相關內建模組。

一、引入

import "regexp"

二、使用

2.1 regexp.matchstring 使用正規表示式匹配字串

match, _ := regexp.matchstring("h(.*)!", "hello world!")

fmt.println(match) // true

2.2 regexp.match 使用正規表示式匹配字串

match, _ := regexp.match("h(.*)!", byte("hello world!"))

fmt.println(match) // true

2.3 regexp.compile 使用正規表示式匹配字串

compile, _ := regexp.compile("h(.*)!")

compilematch := compile.matchstring("hello world!")

fmt.println(compilematch) // true

2.4 compile.findstring 返回匹配到的字串

compile, _ := regexp.compile("h(.*)!")

compilefindstring := compile.findstring("hello world!")

fmt.println(compilefindstring) // hello world!

2.5 compile.find 返回匹配到的字串

compile, _ := regexp.compile("h(.*)!")

compilefind := compile.find(byte("hello world!"))

fmt.println(string(compilefind)) // hello world!

2.6 compile.findstringindex 返回第一次匹配的起始索引

compile, _ := regexp.compile("h(.*)!")

compileindex := compile.findstringindex("hello world!")

fmt.println(compileindex) // [0 12]

未完待續!

Go語言標準庫之flag

go語言內建的flag包實現了命令列引數的解析,flag包使得開發命令列工具更為簡單。如果你只是簡單的想要獲取命令列引數,可以像下面的 示例一樣使用os.args來獲取命令列引數。package main import fmt os os.args demo func main 將上面的 執行go ...

Go語言標準庫之strconv

go語言中strconv包實現了基本資料型別和其字串表示的相互轉換。更多函式請檢視官方文件。這一組函式是我們平時程式設計中用的最多的。將字串型別的整數轉換為int型別。func atoi s string i int,err error 如果傳入的字串引數無法轉換為int型別,就會返回錯誤。s1 1...

Go語言標準庫之time

時間的格式化和解析package main import fmt time func main 輸出結果是 2019 01 01 10 45 502019 01 01 10 45 50 2019年01月01日 10 45 50在系統中還提供了一些預設的格式 ansic mon jan 2 15 04...