github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/regexp/example_test.go (about)

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package regexp_test
     6  
     7  import (
     8  	"fmt"
     9  	"regexp"
    10  )
    11  
    12  func Example() {
    13  	// Compile the expression once, usually at init time.
    14  	// Use raw strings to avoid having to quote the backslashes.
    15  	var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`)
    16  
    17  	fmt.Println(validID.MatchString("adam[23]"))
    18  	fmt.Println(validID.MatchString("eve[7]"))
    19  	fmt.Println(validID.MatchString("Job[48]"))
    20  	fmt.Println(validID.MatchString("snakey"))
    21  	// Output:
    22  	// true
    23  	// true
    24  	// false
    25  	// false
    26  }
    27  
    28  func ExampleMatchString() {
    29  	matched, err := regexp.MatchString("foo.*", "seafood")
    30  	fmt.Println(matched, err)
    31  	matched, err = regexp.MatchString("bar.*", "seafood")
    32  	fmt.Println(matched, err)
    33  	matched, err = regexp.MatchString("a(b", "seafood")
    34  	fmt.Println(matched, err)
    35  	// Output:
    36  	// true <nil>
    37  	// false <nil>
    38  	// false error parsing regexp: missing closing ): `a(b`
    39  }
    40  
    41  func ExampleRegexp_FindString() {
    42  	re := regexp.MustCompile("fo.?")
    43  	fmt.Printf("%q\n", re.FindString("seafood"))
    44  	fmt.Printf("%q\n", re.FindString("meat"))
    45  	// Output:
    46  	// "foo"
    47  	// ""
    48  }
    49  
    50  func ExampleRegexp_FindStringIndex() {
    51  	re := regexp.MustCompile("ab?")
    52  	fmt.Println(re.FindStringIndex("tablett"))
    53  	fmt.Println(re.FindStringIndex("foo") == nil)
    54  	// Output:
    55  	// [1 3]
    56  	// true
    57  }
    58  
    59  func ExampleRegexp_FindStringSubmatch() {
    60  	re := regexp.MustCompile("a(x*)b(y|z)c")
    61  	fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-"))
    62  	fmt.Printf("%q\n", re.FindStringSubmatch("-abzc-"))
    63  	// Output:
    64  	// ["axxxbyc" "xxx" "y"]
    65  	// ["abzc" "" "z"]
    66  }
    67  
    68  func ExampleRegexp_FindAllString() {
    69  	re := regexp.MustCompile("a.")
    70  	fmt.Println(re.FindAllString("paranormal", -1))
    71  	fmt.Println(re.FindAllString("paranormal", 2))
    72  	fmt.Println(re.FindAllString("graal", -1))
    73  	fmt.Println(re.FindAllString("none", -1))
    74  	// Output:
    75  	// [ar an al]
    76  	// [ar an]
    77  	// [aa]
    78  	// []
    79  }
    80  
    81  func ExampleRegexp_FindAllStringSubmatch() {
    82  	re := regexp.MustCompile("a(x*)b")
    83  	fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-", -1))
    84  	fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-", -1))
    85  	fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-axb-", -1))
    86  	fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-ab-", -1))
    87  	// Output:
    88  	// [["ab" ""]]
    89  	// [["axxb" "xx"]]
    90  	// [["ab" ""] ["axb" "x"]]
    91  	// [["axxb" "xx"] ["ab" ""]]
    92  }
    93  
    94  func ExampleRegexp_FindAllStringSubmatchIndex() {
    95  	re := regexp.MustCompile("a(x*)b")
    96  	// Indices:
    97  	//    01234567   012345678
    98  	//    -ab-axb-   -axxb-ab-
    99  	fmt.Println(re.FindAllStringSubmatchIndex("-ab-", -1))
   100  	fmt.Println(re.FindAllStringSubmatchIndex("-axxb-", -1))
   101  	fmt.Println(re.FindAllStringSubmatchIndex("-ab-axb-", -1))
   102  	fmt.Println(re.FindAllStringSubmatchIndex("-axxb-ab-", -1))
   103  	fmt.Println(re.FindAllStringSubmatchIndex("-foo-", -1))
   104  	// Output:
   105  	// [[1 3 2 2]]
   106  	// [[1 5 2 4]]
   107  	// [[1 3 2 2] [4 7 5 6]]
   108  	// [[1 5 2 4] [6 8 7 7]]
   109  	// []
   110  }
   111  
   112  func ExampleRegexp_ReplaceAllLiteralString() {
   113  	re := regexp.MustCompile("a(x*)b")
   114  	fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T"))
   115  	fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "$1"))
   116  	fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "${1}"))
   117  	// Output:
   118  	// -T-T-
   119  	// -$1-$1-
   120  	// -${1}-${1}-
   121  }
   122  
   123  func ExampleRegexp_ReplaceAllString() {
   124  	re := regexp.MustCompile("a(x*)b")
   125  	fmt.Println(re.ReplaceAllString("-ab-axxb-", "T"))
   126  	fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1"))
   127  	fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W"))
   128  	fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W"))
   129  	// Output:
   130  	// -T-T-
   131  	// --xx-
   132  	// ---
   133  	// -W-xxW-
   134  }
   135  
   136  func ExampleRegexp_SubexpNames() {
   137  	re := regexp.MustCompile("(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)")
   138  	fmt.Println(re.MatchString("Alan Turing"))
   139  	fmt.Printf("%q\n", re.SubexpNames())
   140  	reversed := fmt.Sprintf("${%s} ${%s}", re.SubexpNames()[2], re.SubexpNames()[1])
   141  	fmt.Println(reversed)
   142  	fmt.Println(re.ReplaceAllString("Alan Turing", reversed))
   143  	// Output:
   144  	// true
   145  	// ["" "first" "last"]
   146  	// ${last} ${first}
   147  	// Turing Alan
   148  }
   149  
   150  func ExampleRegexp_Split() {
   151  	a := regexp.MustCompile("a")
   152  	fmt.Println(a.Split("banana", -1))
   153  	fmt.Println(a.Split("banana", 0))
   154  	fmt.Println(a.Split("banana", 1))
   155  	fmt.Println(a.Split("banana", 2))
   156  	zp := regexp.MustCompile("z+")
   157  	fmt.Println(zp.Split("pizza", -1))
   158  	fmt.Println(zp.Split("pizza", 0))
   159  	fmt.Println(zp.Split("pizza", 1))
   160  	fmt.Println(zp.Split("pizza", 2))
   161  	// Output:
   162  	// [b n n ]
   163  	// []
   164  	// [banana]
   165  	// [b nana]
   166  	// [pi a]
   167  	// []
   168  	// [pizza]
   169  	// [pi a]
   170  }