Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 1031 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 | 1051 |
1052 // Split slices s into all substrings separated by the expression and returns | 1052 // Split slices s into substrings separated by the expression and returns a slic e of |
1053 // a slice of the substrings between the expression matches. The integer argumen t n | 1053 // the substrings between those expression matches. |
1054 // indicates the maximum number of splits to perform when n >= 0. | |
remyoudompheng
2012/11/13 23:47:08
this looks inconsistent with strings.SplitN
| |
1055 // | 1054 // |
1056 // The slice returned by this method consists of all the substrings of s | 1055 // The slice returned by this method consists of all the substrings of s |
1057 // not contained in the slice returned by FindAllString(). | 1056 // not contained in the slice returned by FindAllString(). When called on an exp ression |
1058 // | 1057 // that contains no metacharacters, it is equivalent to strings.SplitN(). |
1059 // The length of the returned slice is equal to the number of matches found + 1. | 1058 // Example: |
remyoudompheng
2012/11/13 23:47:08
this doesn't look correct, it seems it is rather #
| |
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 | |
1060 func (re *Regexp) Split(s string, n int) []string { | 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 | |
1061 matches := re.FindAllStringIndex(s, n) | 1076 matches := re.FindAllStringIndex(s, n) |
1062 » strings := make([]string, 0, len(matches)+1) | 1077 » strings := make([]string, 0, len(matches)) |
1063 | 1078 |
1064 beg := 0 | 1079 beg := 0 |
1065 end := 0 | 1080 end := 0 |
1066 for _, match := range matches { | 1081 for _, match := range matches { |
1082 if n > 0 && len(strings) >= n-1 { | |
1083 break | |
1084 } | |
1085 | |
1067 end = match[0] | 1086 end = match[0] |
1068 » » strings = append(strings, s[beg:end]) | 1087 » » if match[1] != 0 { |
1088 » » » strings = append(strings, s[beg:end]) | |
1089 » » } | |
1069 beg = match[1] | 1090 beg = match[1] |
1070 } | 1091 } |
1071 » strings = append(strings, s[beg:]) | 1092 |
1093 » if end != len(s) { | |
1094 » » strings = append(strings, s[beg:]) | |
1095 » } | |
1096 | |
1072 return strings | 1097 return strings |
1073 } | 1098 } |
LEFT | RIGHT |