MatchAll find all matches in string for given regexp and returns a list of found matches. If regexp is invalid or string doesn't match regexp, MatchAll fails and returns empty (but non-nil) slice. regexp.Compile is used to construct regexp, and Regexp.FindAllStringSubmatch is used to find matches.
(re string)
| 966 | // m[0].NamedSubmatch("user").IsEqual("john") |
| 967 | // m[1].NamedSubmatch("user").IsEqual("bob") |
| 968 | func (s *String) MatchAll(re string) []Match { |
| 969 | opChain := s.chain.enter("MatchAll()") |
| 970 | defer opChain.leave() |
| 971 | |
| 972 | if opChain.failed() { |
| 973 | return []Match{} |
| 974 | } |
| 975 | |
| 976 | rx, err := regexp.Compile(re) |
| 977 | if err != nil { |
| 978 | opChain.fail(AssertionFailure{ |
| 979 | Type: AssertValid, |
| 980 | Actual: &AssertionValue{re}, |
| 981 | Errors: []error{ |
| 982 | errors.New("expected: valid regexp"), |
| 983 | err, |
| 984 | }, |
| 985 | }) |
| 986 | return []Match{} |
| 987 | } |
| 988 | |
| 989 | matches := rx.FindAllStringSubmatch(s.value, -1) |
| 990 | if matches == nil { |
| 991 | opChain.fail(AssertionFailure{ |
| 992 | Type: AssertMatchRegexp, |
| 993 | Actual: &AssertionValue{s.value}, |
| 994 | Expected: &AssertionValue{re}, |
| 995 | Errors: []error{ |
| 996 | errors.New("expected: string matches regexp"), |
| 997 | }, |
| 998 | }) |
| 999 | return []Match{} |
| 1000 | } |
| 1001 | |
| 1002 | ret := []Match{} |
| 1003 | for _, match := range matches { |
| 1004 | ret = append(ret, *newMatch( |
| 1005 | opChain, |
| 1006 | match, |
| 1007 | rx.SubexpNames())) |
| 1008 | } |
| 1009 | |
| 1010 | return ret |
| 1011 | } |
| 1012 | |
| 1013 | // IsASCII succeeds if all string characters belongs to ASCII. |
| 1014 | // |