Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334085
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: C/C++

2018-01-29 21:06:32

正则表达式实例

点击(此处)折叠或打开

  1. // regexp_my project main.go
  2. package main

  3. import (
  4.     "fmt"
  5.     "regexp"
  6. )

  7. func Example() {
  8.     // Compile the expression once, usually at init time.
  9.     // Use raw strings to avoid having to quote the backslashes.
  10.     var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`)

  11.     fmt.Println(validID.MatchString("adam[23]"))
  12.     fmt.Println(validID.MatchString("eve[7]"))
  13.     fmt.Println(validID.MatchString("Job[48]"))
  14.     fmt.Println(validID.MatchString("snakey"))
  15.     // Output:
  16.     // true
  17.     // true
  18.     // false
  19.     // false
  20. }

  21. func ExampleMatchString() {
  22.     matched, err := regexp.MatchString("foo.*", "seafood")
  23.     fmt.Println(matched, err)
  24.     matched, err = regexp.MatchString("bar.*", "seafood")
  25.     fmt.Println(matched, err)
  26.     matched, err = regexp.MatchString("a(b", "seafood")
  27.     fmt.Println(matched, err)
  28.     // Output:
  29.     // true <nil>
  30.     // false <nil>
  31.     // false error parsing regexp: missing closing ): `a(b`
  32. }

  33. func ExampleRegexp_FindString() {
  34.     re := regexp.MustCompile("foo.?")
  35.     fmt.Printf("%q\n", re.FindString("seafood fool"))
  36.     fmt.Printf("%q\n", re.FindString("meat"))
  37.     // Output:
  38.     // "food"
  39.     // ""
  40. }

  41. func ExampleRegexp_FindStringIndex() {
  42.     re := regexp.MustCompile("ab?")
  43.     fmt.Println(re.FindStringIndex("tablett"))
  44.     fmt.Println(re.FindStringIndex("foo") == nil)
  45.     // Output:
  46.     // [1 3]
  47.     // true
  48. }

  49. func ExampleRegexp_FindStringSubmatch() {
  50.     re := regexp.MustCompile("a(x*)b(y|z)c")
  51.     fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-"))
  52.     fmt.Printf("%q\n", re.FindStringSubmatch("-abzc-"))
  53.     // Output:
  54.     // ["axxxbyc" "xxx" "y"]
  55.     // ["abzc" "" "z"]
  56. }

  57. func ExampleRegexp_FindAllString() {
  58.     re := regexp.MustCompile("a.")
  59.     fmt.Println(re.FindAllString("paranormal", -1))
  60.     fmt.Println(re.FindAllString("paranormal", 2))
  61.     fmt.Println(re.FindAllString("graal", -1))
  62.     fmt.Println(re.FindAllString("none", -1))
  63.     // Output:
  64.     // [ar an al]
  65.     // [ar an]
  66.     // [aa]
  67.     // []
  68. }

  69. func ExampleRegexp_FindAllStringSubmatch() {
  70.     re := regexp.MustCompile("a(x*)b")
  71.     fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-", -1))
  72.     fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-", -1))
  73.     fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-axb-", -1))
  74.     fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-ab-", -1))
  75.     // Output:
  76.     // [["ab" ""]]
  77.     // [["axxb" "xx"]]
  78.     // [["ab" ""] ["axb" "x"]]
  79.     // [["axxb" "xx"] ["ab" ""]]
  80. }

  81. func ExampleRegexp_FindAllStringSubmatchIndex() {
  82.     re := regexp.MustCompile("a(x*)b")
  83.     // Indices:
  84.     // 01234567 012345678
  85.     // -ab-axb- -axxb-ab-
  86.     fmt.Println(re.FindAllStringSubmatchIndex("-ab-", -1))
  87.     fmt.Println(re.FindAllStringSubmatchIndex("-axxb-", -1))
  88.     fmt.Println(re.FindAllStringSubmatchIndex("-ab-axb-", -1))
  89.     fmt.Println(re.FindAllStringSubmatchIndex("-axxb-ab-", -1))
  90.     fmt.Println(re.FindAllStringSubmatchIndex("-foo-", -1))
  91.     // Output:
  92.     // [[1 3 2 2]]
  93.     // [[1 5 2 4]]
  94.     // [[1 3 2 2] [4 7 5 6]]
  95.     // [[1 5 2 4] [6 8 7 7]]
  96.     // []
  97. }

  98. func ExampleRegexp_MatchString() {
  99.     re := regexp.MustCompile("(gopher){2}")
  100.     fmt.Println(re.MatchString("gopher"))
  101.     fmt.Println(re.MatchString("gophergopher"))
  102.     fmt.Println(re.MatchString("gophergophergopher"))
  103.     // Output:
  104.     // false
  105.     // true
  106.     // true
  107. }

  108. func ExampleRegexp_ReplaceAllLiteralString() {
  109.     re := regexp.MustCompile("a(x*)b")
  110.     fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T"))
  111.     fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "$1"))
  112.     fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "${1}"))
  113.     // Output:
  114.     // -T-T-
  115.     // -$1-$1-
  116.     // -${1}-${1}-
  117. }

  118. func ExampleRegexp_ReplaceAllString() {
  119.     re := regexp.MustCompile("a(x*)b")
  120.     fmt.Println(re.ReplaceAllString("-ab-axxb-", "T"))
  121.     fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1"))
  122.     fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W"))
  123.     fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W"))
  124.     // Output:
  125.     // -T-T-
  126.     // --xx-
  127.     // ---
  128.     // -W-xxW-
  129. }

  130. func ExampleRegexp_SubexpNames() {
  131.     re := regexp.MustCompile("(?P[a-zA-Z]+) (?P[a-zA-Z]+)")
  132.     fmt.Println(re.MatchString("Alan Turing"))
  133.     fmt.Printf("%q\n", re.SubexpNames())
  134.     reversed := fmt.Sprintf("${%s} ${%s}", re.SubexpNames()[2], re.SubexpNames()[1])
  135.     fmt.Println(reversed)
  136.     fmt.Println(re.ReplaceAllString("Alan Turing", reversed))
  137.     // Output:
  138.     // true
  139.     // ["" "first" "last"]
  140.     // ${last} ${first}
  141.     // Turing Alan
  142. }

  143. func ExampleRegexp_Split() {
  144.     a := regexp.MustCompile("a")
  145.     fmt.Println(a.Split("banana", -1))
  146.     fmt.Println(a.Split("banana", 0))
  147.     fmt.Println(a.Split("banana", 1))
  148.     fmt.Println(a.Split("banana", 2))
  149.     zp := regexp.MustCompile("z+")
  150.     fmt.Println(zp.Split("pizza", -1))
  151.     fmt.Println(zp.Split("pizza", 0))
  152.     fmt.Println(zp.Split("pizza", 1))
  153.     fmt.Println(zp.Split("pizza", 2))
  154.     // Output:
  155.     // [b n n ]
  156.     // []
  157.     // [banana]
  158.     // [b nana]
  159.     // [pi a]
  160.     // []
  161.     // [pizza]
  162.     // [pi a]
  163. }

  164. func main() {
  165.     Example()
  166.     ExampleMatchString()
  167.     ExampleRegexp_FindString()
  168.     ExampleRegexp_FindStringIndex()
  169.     ExampleRegexp_FindStringSubmatch()
  170.     ExampleRegexp_FindAllString()
  171.     ExampleRegexp_FindAllStringSubmatch()
  172.     ExampleRegexp_FindAllStringSubmatchIndex()
  173.     ExampleRegexp_MatchString()
  174.     ExampleRegexp_ReplaceAllLiteralString()
  175.     ExampleRegexp_ReplaceAllString()
  176.     ExampleRegexp_SubexpNames()
  177.     ExampleRegexp_Split()
  178. }
结果:
true

true

false

false

true

false

false error parsing regexp: missing closing ): `a(b`

"food"

""

[1 3]

true

["axxxbyc" "xxx" "y"]

["abzc" "" "z"]

[ar an al]

[ar an]

[aa]

[]

[["ab" ""]]

[["axxb" "xx"]]

[["ab" ""] ["axb" "x"]]

[["axxb" "xx"] ["ab" ""]]

[[1 3 2 2]]

[[1 5 2 4]]

[[1 3 2 2] [4 7 5 6]]

[[1 5 2 4] [6 8 7 7]]

[]

false

true

true

-T-T-

-$1-$1-

-${1}-${1}-

-T-T-

--xx-

---

-W-xxW-

true

["" "first" "last"]

${last} ${first}

Turing Alan

[b n n ]

[]

[banana]

[b nana]

[pi a]

[]

[pizza]

[pi a]

成功: 进程退出代码 0.




阅读(325) | 评论(0) | 转发(0) |
0

上一篇:文件路径分割

下一篇:时间处理

给主人留下些什么吧!~~