OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package regexp implements regular expression search. | 5 // Package regexp implements regular expression search. |
6 // | 6 // |
7 // The syntax of the regular expressions accepted is the same | 7 // The syntax of the regular expressions accepted is the same |
8 // general syntax used by Perl, Python, and other languages. | 8 // general syntax used by Perl, Python, and other languages. |
9 // More precisely, it is the syntax accepted by RE2 and described at | 9 // More precisely, it is the syntax accepted by RE2 and described at |
10 // http://code.google.com/p/re2/wiki/Syntax, except for \C. | 10 // http://code.google.com/p/re2/wiki/Syntax, except for \C. |
(...skipping 1030 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1041 } | 1041 } |
1042 result := make([][]int, 0, startSize) | 1042 result := make([][]int, 0, startSize) |
1043 re.allMatches(s, nil, n, func(match []int) { | 1043 re.allMatches(s, nil, n, func(match []int) { |
1044 result = append(result, match) | 1044 result = append(result, match) |
1045 }) | 1045 }) |
1046 if len(result) == 0 { | 1046 if len(result) == 0 { |
1047 return nil | 1047 return nil |
1048 } | 1048 } |
1049 return result | 1049 return result |
1050 } | 1050 } |
| 1051 |
| 1052 // Split slices s into substrings separated by the expression and returns a slic
e of |
| 1053 // the substrings between those expression matches. |
| 1054 // |
| 1055 // The slice returned by this method consists of all the substrings of s |
| 1056 // not contained in the slice returned by FindAllString(). When called on an exp
ression |
| 1057 // that contains no metacharacters, it is equivalent to strings.SplitN(). |
| 1058 // Example: |
| 1059 // s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5) |
| 1060 // // s: ["", "b", "b", "c", "cadaaae"] |
| 1061 // |
| 1062 // The count determines the number of substrings to return: |
| 1063 // n > 0: at most n substrings; the last substring will be the unsplit remaind
er. |
| 1064 // n == 0: the result is nil (zero substrings) |
| 1065 // n < 0: all substrings |
| 1066 func (re *Regexp) Split(s string, n int) []string { |
| 1067 |
| 1068 if n == 0 { |
| 1069 return nil |
| 1070 } |
| 1071 |
| 1072 if len(re.expr) > 0 && len(s) == 0 { |
| 1073 return []string{""} |
| 1074 } |
| 1075 |
| 1076 matches := re.FindAllStringIndex(s, n) |
| 1077 strings := make([]string, 0, len(matches)) |
| 1078 |
| 1079 beg := 0 |
| 1080 end := 0 |
| 1081 for _, match := range matches { |
| 1082 if n > 0 && len(strings) >= n-1 { |
| 1083 break |
| 1084 } |
| 1085 |
| 1086 end = match[0] |
| 1087 if match[1] != 0 { |
| 1088 strings = append(strings, s[beg:end]) |
| 1089 } |
| 1090 beg = match[1] |
| 1091 } |
| 1092 |
| 1093 if end != len(s) { |
| 1094 strings = append(strings, s[beg:]) |
| 1095 } |
| 1096 |
| 1097 return strings |
| 1098 } |
OLD | NEW |