在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ GO/ Go正則表達式實例
Go panic錯誤處理實例
Go命令行參數(shù)實例
Go可變參數(shù)的函數(shù)實例
Go通道同步實例
Go非阻塞通道操作實例
Go指針實例
Go數(shù)字解析實例
Go語言指針
Go超時(timeouts)實例
Go速率限制實例
Go信號實例
Go Base64編碼實例
Go計時器實例
Go命令行標志實例
Go原子計數(shù)器實例
Go語言切片
Go隨機數(shù)實例
Go語言類型轉換
Go排序實例
Go時間格式化/解析實例
Go URL解析實例
Go字符串函數(shù)實例
Go語言常量
Go for循環(huán)語句實例
Go函數(shù)多個返回值實例
Go切片實例
Go行過濾器實例
Go語言接口
Go語言數(shù)組
Go語言變量
Go字符串格式化實例
Go斷續(xù)器實例
Go if/else語句實例
Go通道緩沖實例
Go錯誤實例
Go語言映射
Go執(zhí)行過程實例
Go函數(shù)實例
Go有狀態(tài)的goroutines實例
Go按自定義函數(shù)排序實例
Go語言作用域規(guī)則
Go時代(Epoch)實例
Go變量實例
Go互斥體實例
Go語言范圍(range)
Go程序實例
Go語言入門
Go通道路線實例
Go閉包(匿名函數(shù))實例
Go Select實例
Go通道范圍實例
Go集合函數(shù)實例
Hello World程序實例
Go環(huán)境變量實例
Go語言運算符
Go讀取文件實例
Go延遲(defer)實例
Go SHA1哈希實例
Go語言條件和決策
Go語言錯誤處理
Go通道實例
Go指針實例
Go時間日期實例
Go語言字符串
Go語言循環(huán)
Go語言基礎語法
Go語言開發(fā)環(huán)境安裝配置
Go常量實例
Go語言結構體
Go寫文件實例
Go正則表達式實例
Go JSON實例
Go語言教程
Go關閉通道實例
Go接口實例
Go語言遞歸
Go switch語句實例
Go函數(shù)遞歸實例
Go退出程序實例
Go語言程序結構
Go范圍實例
Go語言函數(shù)
Go工作池實例
Go語言數(shù)據(jù)類型

Go正則表達式實例

Go提供對正則表達式的內置支持。 下面是Go中常見的regexp相關任務的一些例子。

具體的每個函數(shù),可參考示例中的代碼 -

所有的示例代碼,都放在 F:\worksp\golang 目錄下。安裝Go編程環(huán)境請參考:http://www.yiibai.com/go/go_environment.html

string-formatting.go的完整代碼如下所示 -

package main

import "bytes"
import "fmt"
import "regexp"

func main() {

    // This tests whether a pattern matches a string.
    match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
    fmt.Println(match)

    // Above we used a string pattern directly, but for
    // other regexp tasks you'll need to `Compile` an
    // optimized `Regexp` struct.
    r, _ := regexp.Compile("p([a-z]+)ch")

    // Many methods are available on these structs. Here's
    // a match test like we saw earlier.
    fmt.Println(r.MatchString("peach"))

    // This finds the match for the regexp.
    fmt.Println(r.FindString("peach punch"))

    // This also finds the first match but returns the
    // start and end indexes for the match instead of the
    // matching text.
    fmt.Println(r.FindStringIndex("peach punch"))

    // The `Submatch` variants include information about
    // both the whole-pattern matches and the submatches
    // within those matches. For example this will return
    // information for both `p([a-z]+)ch` and `([a-z]+)`.
    fmt.Println(r.FindStringSubmatch("peach punch"))

    // Similarly this will return information about the
    // indexes of matches and submatches.
    fmt.Println(r.FindStringSubmatchIndex("peach punch"))

    // The `All` variants of these functions apply to all
    // matches in the input, not just the first. For
    // example to find all matches for a regexp.
    fmt.Println(r.FindAllString("peach punch pinch", -1))

    // These `All` variants are available for the other
    // functions we saw above as well.
    fmt.Println(r.FindAllStringSubmatchIndex(
        "peach punch pinch", -1))

    // Providing a non-negative integer as the second
    // argument to these functions will limit the number
    // of matches.
    fmt.Println(r.FindAllString("peach punch pinch", 2))

    // Our examples above had string arguments and used
    // names like `MatchString`. We can also provide
    // `[]byte` arguments and drop `String` from the
    // function name.
    fmt.Println(r.Match([]byte("peach")))

    // When creating constants with regular expressions
    // you can use the `MustCompile` variation of
    // `Compile`. A plain `Compile` won't work for
    // constants because it has 2 return values.
    r = regexp.MustCompile("p([a-z]+)ch")
    fmt.Println(r)

    // The `regexp` package can also be used to replace
    // subsets of strings with other values.
    fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))

    // The `Func` variant allows you to transform matched
    // text with a given function.
    in := []byte("a peach")
    out := r.ReplaceAllFunc(in, bytes.ToUpper)
    fmt.Println(string(out))
}

執(zhí)行上面代碼,將得到以下輸出結果 -

F:\worksp\golang>go run regular-expressions.go
true
true
peach
[0 5]
[peach ea]
[0 5 1 3]
[peach punch pinch]
[[0 5 1 3] [6 11 7 9] [12 17 13 15]]
[peach punch]
true
p([a-z]+)ch
a <fruit>
a PEACH