Match matches the string with given regexp and returns a new Match instance with found submatches. If regexp is invalid or string doesn't match regexp, Match fails and returns empty (but non-nil) instance. regexp.Compile is used to construct regexp, and Regexp.FindStringSubmatch is used to construc
(re string)
| 872 | // m.NamedSubmatch("host").IsEqual("example.com") |
| 873 | // m.NamedSubmatch("user").IsEqual("john") |
| 874 | func (s *String) Match(re string) *Match { |
| 875 | opChain := s.chain.enter("Match()") |
| 876 | defer opChain.leave() |
| 877 | |
| 878 | if opChain.failed() { |
| 879 | return newMatch(opChain, nil, nil) |
| 880 | } |
| 881 | |
| 882 | rx, err := regexp.Compile(re) |
| 883 | if err != nil { |
| 884 | opChain.fail(AssertionFailure{ |
| 885 | Type: AssertValid, |
| 886 | Actual: &AssertionValue{re}, |
| 887 | Errors: []error{ |
| 888 | errors.New("expected: valid regexp"), |
| 889 | err, |
| 890 | }, |
| 891 | }) |
| 892 | return newMatch(opChain, nil, nil) |
| 893 | } |
| 894 | |
| 895 | match := rx.FindStringSubmatch(s.value) |
| 896 | if match == nil { |
| 897 | opChain.fail(AssertionFailure{ |
| 898 | Type: AssertMatchRegexp, |
| 899 | Actual: &AssertionValue{s.value}, |
| 900 | Expected: &AssertionValue{re}, |
| 901 | Errors: []error{ |
| 902 | errors.New("expected: string matches regexp"), |
| 903 | }, |
| 904 | }) |
| 905 | return newMatch(opChain, nil, nil) |
| 906 | } |
| 907 | |
| 908 | return newMatch(opChain, match, rx.SubexpNames()) |
| 909 | } |
| 910 | |
| 911 | // NotMatch succeeds if the string doesn't match to given regexp. |
| 912 | // |