github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/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 ExampleQuoteMeta() { 42 fmt.Println(regexp.QuoteMeta("Escaping symbols like: .+*?()|[]{}^$")) 43 // Output: 44 // Escaping symbols like: \.\+\*\?\(\)\|\[\]\{\}\^\$ 45 } 46 47 func ExampleRegexp_FindString() { 48 re := regexp.MustCompile("foo.?") 49 fmt.Printf("%q\n", re.FindString("seafood fool")) 50 fmt.Printf("%q\n", re.FindString("meat")) 51 // Output: 52 // "food" 53 // "" 54 } 55 56 func ExampleRegexp_FindStringIndex() { 57 re := regexp.MustCompile("ab?") 58 fmt.Println(re.FindStringIndex("tablett")) 59 fmt.Println(re.FindStringIndex("foo") == nil) 60 // Output: 61 // [1 3] 62 // true 63 } 64 65 func ExampleRegexp_FindStringSubmatch() { 66 re := regexp.MustCompile("a(x*)b(y|z)c") 67 fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-")) 68 fmt.Printf("%q\n", re.FindStringSubmatch("-abzc-")) 69 // Output: 70 // ["axxxbyc" "xxx" "y"] 71 // ["abzc" "" "z"] 72 } 73 74 func ExampleRegexp_FindAllString() { 75 re := regexp.MustCompile("a.") 76 fmt.Println(re.FindAllString("paranormal", -1)) 77 fmt.Println(re.FindAllString("paranormal", 2)) 78 fmt.Println(re.FindAllString("graal", -1)) 79 fmt.Println(re.FindAllString("none", -1)) 80 // Output: 81 // [ar an al] 82 // [ar an] 83 // [aa] 84 // [] 85 } 86 87 func ExampleRegexp_FindAllStringSubmatch() { 88 re := regexp.MustCompile("a(x*)b") 89 fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-", -1)) 90 fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-", -1)) 91 fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-axb-", -1)) 92 fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-ab-", -1)) 93 // Output: 94 // [["ab" ""]] 95 // [["axxb" "xx"]] 96 // [["ab" ""] ["axb" "x"]] 97 // [["axxb" "xx"] ["ab" ""]] 98 } 99 100 func ExampleRegexp_FindAllStringSubmatchIndex() { 101 re := regexp.MustCompile("a(x*)b") 102 // Indices: 103 // 01234567 012345678 104 // -ab-axb- -axxb-ab- 105 fmt.Println(re.FindAllStringSubmatchIndex("-ab-", -1)) 106 fmt.Println(re.FindAllStringSubmatchIndex("-axxb-", -1)) 107 fmt.Println(re.FindAllStringSubmatchIndex("-ab-axb-", -1)) 108 fmt.Println(re.FindAllStringSubmatchIndex("-axxb-ab-", -1)) 109 fmt.Println(re.FindAllStringSubmatchIndex("-foo-", -1)) 110 // Output: 111 // [[1 3 2 2]] 112 // [[1 5 2 4]] 113 // [[1 3 2 2] [4 7 5 6]] 114 // [[1 5 2 4] [6 8 7 7]] 115 // [] 116 } 117 118 func ExampleRegexp_MatchString() { 119 re := regexp.MustCompile("(gopher){2}") 120 fmt.Println(re.MatchString("gopher")) 121 fmt.Println(re.MatchString("gophergopher")) 122 fmt.Println(re.MatchString("gophergophergopher")) 123 // Output: 124 // false 125 // true 126 // true 127 } 128 129 func ExampleRegexp_ReplaceAllLiteralString() { 130 re := regexp.MustCompile("a(x*)b") 131 fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T")) 132 fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "$1")) 133 fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "${1}")) 134 // Output: 135 // -T-T- 136 // -$1-$1- 137 // -${1}-${1}- 138 } 139 140 func ExampleRegexp_ReplaceAllString() { 141 re := regexp.MustCompile("a(x*)b") 142 fmt.Println(re.ReplaceAllString("-ab-axxb-", "T")) 143 fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1")) 144 fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W")) 145 fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W")) 146 // Output: 147 // -T-T- 148 // --xx- 149 // --- 150 // -W-xxW- 151 } 152 153 func ExampleRegexp_SubexpNames() { 154 re := regexp.MustCompile("(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)") 155 fmt.Println(re.MatchString("Alan Turing")) 156 fmt.Printf("%q\n", re.SubexpNames()) 157 reversed := fmt.Sprintf("${%s} ${%s}", re.SubexpNames()[2], re.SubexpNames()[1]) 158 fmt.Println(reversed) 159 fmt.Println(re.ReplaceAllString("Alan Turing", reversed)) 160 // Output: 161 // true 162 // ["" "first" "last"] 163 // ${last} ${first} 164 // Turing Alan 165 } 166 167 func ExampleRegexp_Split() { 168 a := regexp.MustCompile("a") 169 fmt.Println(a.Split("banana", -1)) 170 fmt.Println(a.Split("banana", 0)) 171 fmt.Println(a.Split("banana", 1)) 172 fmt.Println(a.Split("banana", 2)) 173 zp := regexp.MustCompile("z+") 174 fmt.Println(zp.Split("pizza", -1)) 175 fmt.Println(zp.Split("pizza", 0)) 176 fmt.Println(zp.Split("pizza", 1)) 177 fmt.Println(zp.Split("pizza", 2)) 178 // Output: 179 // [b n n ] 180 // [] 181 // [banana] 182 // [b nana] 183 // [pi a] 184 // [] 185 // [pizza] 186 // [pi a] 187 } 188 189 func ExampleRegexp_Expand() { 190 content := []byte(` 191 # comment line 192 option1: value1 193 option2: value2 194 195 # another comment line 196 option3: value3 197 `) 198 199 // Regex pattern captures "key: value" pair from the content. 200 pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) 201 202 // Template to convert "key: value" to "key=value" by 203 // referencing the values captured by the regex pattern. 204 template := []byte("$key=$value\n") 205 206 result := []byte{} 207 208 // For each match of the regex in the content. 209 for _, submatches := range pattern.FindAllSubmatchIndex(content, -1) { 210 // Apply the captured submatches to the template and append the output 211 // to the result. 212 result = pattern.Expand(result, template, content, submatches) 213 } 214 fmt.Println(string(result)) 215 // Output: 216 // option1=value1 217 // option2=value2 218 // option3=value3 219 } 220 221 func ExampleRegexp_ExpandString() { 222 content := ` 223 # comment line 224 option1: value1 225 option2: value2 226 227 # another comment line 228 option3: value3 229 ` 230 231 // Regex pattern captures "key: value" pair from the content. 232 pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) 233 234 // Template to convert "key: value" to "key=value" by 235 // referencing the values captured by the regex pattern. 236 template := "$key=$value\n" 237 238 result := []byte{} 239 240 // For each match of the regex in the content. 241 for _, submatches := range pattern.FindAllStringSubmatchIndex(content, -1) { 242 // Apply the captured submatches to the template and append the output 243 // to the result. 244 result = pattern.ExpandString(result, template, content, submatches) 245 } 246 fmt.Println(string(result)) 247 // Output: 248 // option1=value1 249 // option2=value2 250 // option3=value3 251 } 252 253 func ExampleRegexp_FindIndex() { 254 content := []byte(` 255 # comment line 256 option1: value1 257 option2: value2 258 `) 259 // Regex pattern captures "key: value" pair from the content. 260 pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) 261 262 loc := pattern.FindIndex(content) 263 fmt.Println(loc) 264 fmt.Println(string(content[loc[0]:loc[1]])) 265 // Output: 266 // [18 33] 267 // option1: value1 268 } 269 func ExampleRegexp_FindAllSubmatchIndex() { 270 content := []byte(` 271 # comment line 272 option1: value1 273 option2: value2 274 `) 275 // Regex pattern captures "key: value" pair from the content. 276 pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) 277 allIndexes := pattern.FindAllSubmatchIndex(content, -1) 278 for _, loc := range allIndexes { 279 fmt.Println(loc) 280 fmt.Println(string(content[loc[0]:loc[1]])) 281 fmt.Println(string(content[loc[2]:loc[3]])) 282 fmt.Println(string(content[loc[4]:loc[5]])) 283 } 284 // Output: 285 // [18 33 18 25 27 33] 286 // option1: value1 287 // option1 288 // value1 289 // [35 50 35 42 44 50] 290 // option2: value2 291 // option2 292 // value2 293 }