zoukankan      html  css  js  c++  java
  • Go语言标准库之regexp

    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]

    未完待续!

  • 相关阅读:
    Xn数列(codevs 1281)
    素数密度(洛谷 1835)
    Sightseeing(poj 3463)
    线段树——Ultra-QuickSort
    最大子矩阵
    完成作业的先后顺序
    堆积木块的最大高度
    最长上升序列和
    最长上升子序列
    高精度乘法程序
  • 原文地址:https://www.cnblogs.com/aaronthon/p/13540097.html
Copyright © 2011-2022 走看看