| 1 |
// Copyright (c) 2019, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package pattern_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"regexp" |
| 9 |
|
| 10 |
"mvdan.cc/sh/v3/pattern" |
| 11 |
) |
| 12 |
|
| 13 |
func ExampleRegexp() { |
| 14 |
pat := "foo?bar*" |
| 15 |
fmt.Println(pat) |
| 16 |
|
| 17 |
expr, err := pattern.Regexp(pat, 0) |
| 18 |
if err != nil { |
| 19 |
return |
| 20 |
} |
| 21 |
fmt.Println(expr) |
| 22 |
|
| 23 |
rx := regexp.MustCompile(expr) |
| 24 |
fmt.Println(rx.MatchString("foo bar baz")) |
| 25 |
fmt.Println(rx.MatchString("foobarbaz")) |
| 26 |
// Output: |
| 27 |
// foo?bar* |
| 28 |
// (?s)foo.bar.* |
| 29 |
// true |
| 30 |
// false |
| 31 |
} |
| 32 |
|
| 33 |
func ExampleQuoteMeta() { |
| 34 |
pat := "foo?bar*" |
| 35 |
const mode = 0 |
| 36 |
fmt.Println(pat) |
| 37 |
|
| 38 |
quoted := pattern.QuoteMeta(pat, mode) |
| 39 |
fmt.Println(quoted) |
| 40 |
|
| 41 |
expr, err := pattern.Regexp(quoted, mode) |
| 42 |
if err != nil { |
| 43 |
return |
| 44 |
} |
| 45 |
|
| 46 |
rx := regexp.MustCompile(expr) |
| 47 |
fmt.Println(rx.MatchString("foo bar baz")) |
| 48 |
fmt.Println(rx.MatchString("foo?bar*")) |
| 49 |
// Output: |
| 50 |
// foo?bar* |
| 51 |
// foo\?bar\* |
| 52 |
// false |
| 53 |
// true |
| 54 |
} |