Source file
src/regexp/regexp.go
Documentation: regexp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 package regexp
67
68 import (
69 "bytes"
70 "io"
71 "regexp/syntax"
72 "strconv"
73 "strings"
74 "sync"
75 "unicode"
76 "unicode/utf8"
77 )
78
79
80
81
82 type Regexp struct {
83 expr string
84 prog *syntax.Prog
85 onepass *onePassProg
86 numSubexp int
87 maxBitStateLen int
88 subexpNames []string
89 prefix string
90 prefixBytes []byte
91 prefixRune rune
92 prefixEnd uint32
93 mpool int
94 matchcap int
95 prefixComplete bool
96 cond syntax.EmptyOp
97 minInputLen int
98
99
100
101 longest bool
102 }
103
104
105 func (re *Regexp) String() string {
106 return re.expr
107 }
108
109
110
111
112
113
114
115
116
117 func (re *Regexp) Copy() *Regexp {
118 re2 := *re
119 return &re2
120 }
121
122
123
124
125
126
127
128
129
130
131
132 func Compile(expr string) (*Regexp, error) {
133 return compile(expr, syntax.Perl, false)
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 func CompilePOSIX(expr string) (*Regexp, error) {
156 return compile(expr, syntax.POSIX, true)
157 }
158
159
160
161
162
163
164
165 func (re *Regexp) Longest() {
166 re.longest = true
167 }
168
169 func compile(expr string, mode syntax.Flags, longest bool) (*Regexp, error) {
170 re, err := syntax.Parse(expr, mode)
171 if err != nil {
172 return nil, err
173 }
174 maxCap := re.MaxCap()
175 capNames := re.CapNames()
176
177 re = re.Simplify()
178 prog, err := syntax.Compile(re)
179 if err != nil {
180 return nil, err
181 }
182 matchcap := prog.NumCap
183 if matchcap < 2 {
184 matchcap = 2
185 }
186 regexp := &Regexp{
187 expr: expr,
188 prog: prog,
189 onepass: compileOnePass(prog),
190 numSubexp: maxCap,
191 subexpNames: capNames,
192 cond: prog.StartCond(),
193 longest: longest,
194 matchcap: matchcap,
195 minInputLen: minInputLen(re),
196 }
197 if regexp.onepass == nil {
198 regexp.prefix, regexp.prefixComplete = prog.Prefix()
199 regexp.maxBitStateLen = maxBitStateLen(prog)
200 } else {
201 regexp.prefix, regexp.prefixComplete, regexp.prefixEnd = onePassPrefix(prog)
202 }
203 if regexp.prefix != "" {
204
205
206 regexp.prefixBytes = []byte(regexp.prefix)
207 regexp.prefixRune, _ = utf8.DecodeRuneInString(regexp.prefix)
208 }
209
210 n := len(prog.Inst)
211 i := 0
212 for matchSize[i] != 0 && matchSize[i] < n {
213 i++
214 }
215 regexp.mpool = i
216
217 return regexp, nil
218 }
219
220
221
222
223
224
225
226 var (
227 matchSize = [...]int{128, 512, 2048, 16384, 0}
228 matchPool [len(matchSize)]sync.Pool
229 )
230
231
232
233
234 func (re *Regexp) get() *machine {
235 m, ok := matchPool[re.mpool].Get().(*machine)
236 if !ok {
237 m = new(machine)
238 }
239 m.re = re
240 m.p = re.prog
241 if cap(m.matchcap) < re.matchcap {
242 m.matchcap = make([]int, re.matchcap)
243 for _, t := range m.pool {
244 t.cap = make([]int, re.matchcap)
245 }
246 }
247
248
249
250 n := matchSize[re.mpool]
251 if n == 0 {
252 n = len(re.prog.Inst)
253 }
254 if len(m.q0.sparse) < n {
255 m.q0 = queue{make([]uint32, n), make([]entry, 0, n)}
256 m.q1 = queue{make([]uint32, n), make([]entry, 0, n)}
257 }
258 return m
259 }
260
261
262 func (re *Regexp) put(m *machine) {
263 m.re = nil
264 m.p = nil
265 m.inputs.clear()
266 matchPool[re.mpool].Put(m)
267 }
268
269
270 func minInputLen(re *syntax.Regexp) int {
271 switch re.Op {
272 default:
273 return 0
274 case syntax.OpAnyChar, syntax.OpAnyCharNotNL, syntax.OpCharClass:
275 return 1
276 case syntax.OpLiteral:
277 l := 0
278 for _, r := range re.Rune {
279 l += utf8.RuneLen(r)
280 }
281 return l
282 case syntax.OpCapture, syntax.OpPlus:
283 return minInputLen(re.Sub[0])
284 case syntax.OpRepeat:
285 return re.Min * minInputLen(re.Sub[0])
286 case syntax.OpConcat:
287 l := 0
288 for _, sub := range re.Sub {
289 l += minInputLen(sub)
290 }
291 return l
292 case syntax.OpAlternate:
293 l := minInputLen(re.Sub[0])
294 var lnext int
295 for _, sub := range re.Sub[1:] {
296 lnext = minInputLen(sub)
297 if lnext < l {
298 l = lnext
299 }
300 }
301 return l
302 }
303 }
304
305
306
307
308 func MustCompile(str string) *Regexp {
309 regexp, err := Compile(str)
310 if err != nil {
311 panic(`regexp: Compile(` + quote(str) + `): ` + err.Error())
312 }
313 return regexp
314 }
315
316
317
318
319 func MustCompilePOSIX(str string) *Regexp {
320 regexp, err := CompilePOSIX(str)
321 if err != nil {
322 panic(`regexp: CompilePOSIX(` + quote(str) + `): ` + err.Error())
323 }
324 return regexp
325 }
326
327 func quote(s string) string {
328 if strconv.CanBackquote(s) {
329 return "`" + s + "`"
330 }
331 return strconv.Quote(s)
332 }
333
334
335 func (re *Regexp) NumSubexp() int {
336 return re.numSubexp
337 }
338
339
340
341
342
343
344 func (re *Regexp) SubexpNames() []string {
345 return re.subexpNames
346 }
347
348
349
350
351
352
353
354
355 func (re *Regexp) SubexpIndex(name string) int {
356 if name != "" {
357 for i, s := range re.subexpNames {
358 if name == s {
359 return i
360 }
361 }
362 }
363 return -1
364 }
365
366 const endOfText rune = -1
367
368
369
370 type input interface {
371 step(pos int) (r rune, width int)
372 canCheckPrefix() bool
373 hasPrefix(re *Regexp) bool
374 index(re *Regexp, pos int) int
375 context(pos int) lazyFlag
376 }
377
378
379 type inputString struct {
380 str string
381 }
382
383 func (i *inputString) step(pos int) (rune, int) {
384 if pos < len(i.str) {
385 c := i.str[pos]
386 if c < utf8.RuneSelf {
387 return rune(c), 1
388 }
389 return utf8.DecodeRuneInString(i.str[pos:])
390 }
391 return endOfText, 0
392 }
393
394 func (i *inputString) canCheckPrefix() bool {
395 return true
396 }
397
398 func (i *inputString) hasPrefix(re *Regexp) bool {
399 return strings.HasPrefix(i.str, re.prefix)
400 }
401
402 func (i *inputString) index(re *Regexp, pos int) int {
403 return strings.Index(i.str[pos:], re.prefix)
404 }
405
406 func (i *inputString) context(pos int) lazyFlag {
407 r1, r2 := endOfText, endOfText
408
409 if uint(pos-1) < uint(len(i.str)) {
410 r1 = rune(i.str[pos-1])
411 if r1 >= utf8.RuneSelf {
412 r1, _ = utf8.DecodeLastRuneInString(i.str[:pos])
413 }
414 }
415
416 if uint(pos) < uint(len(i.str)) {
417 r2 = rune(i.str[pos])
418 if r2 >= utf8.RuneSelf {
419 r2, _ = utf8.DecodeRuneInString(i.str[pos:])
420 }
421 }
422 return newLazyFlag(r1, r2)
423 }
424
425
426 type inputBytes struct {
427 str []byte
428 }
429
430 func (i *inputBytes) step(pos int) (rune, int) {
431 if pos < len(i.str) {
432 c := i.str[pos]
433 if c < utf8.RuneSelf {
434 return rune(c), 1
435 }
436 return utf8.DecodeRune(i.str[pos:])
437 }
438 return endOfText, 0
439 }
440
441 func (i *inputBytes) canCheckPrefix() bool {
442 return true
443 }
444
445 func (i *inputBytes) hasPrefix(re *Regexp) bool {
446 return bytes.HasPrefix(i.str, re.prefixBytes)
447 }
448
449 func (i *inputBytes) index(re *Regexp, pos int) int {
450 return bytes.Index(i.str[pos:], re.prefixBytes)
451 }
452
453 func (i *inputBytes) context(pos int) lazyFlag {
454 r1, r2 := endOfText, endOfText
455
456 if uint(pos-1) < uint(len(i.str)) {
457 r1 = rune(i.str[pos-1])
458 if r1 >= utf8.RuneSelf {
459 r1, _ = utf8.DecodeLastRune(i.str[:pos])
460 }
461 }
462
463 if uint(pos) < uint(len(i.str)) {
464 r2 = rune(i.str[pos])
465 if r2 >= utf8.RuneSelf {
466 r2, _ = utf8.DecodeRune(i.str[pos:])
467 }
468 }
469 return newLazyFlag(r1, r2)
470 }
471
472
473 type inputReader struct {
474 r io.RuneReader
475 atEOT bool
476 pos int
477 }
478
479 func (i *inputReader) step(pos int) (rune, int) {
480 if !i.atEOT && pos != i.pos {
481 return endOfText, 0
482
483 }
484 r, w, err := i.r.ReadRune()
485 if err != nil {
486 i.atEOT = true
487 return endOfText, 0
488 }
489 i.pos += w
490 return r, w
491 }
492
493 func (i *inputReader) canCheckPrefix() bool {
494 return false
495 }
496
497 func (i *inputReader) hasPrefix(re *Regexp) bool {
498 return false
499 }
500
501 func (i *inputReader) index(re *Regexp, pos int) int {
502 return -1
503 }
504
505 func (i *inputReader) context(pos int) lazyFlag {
506 return 0
507 }
508
509
510
511
512 func (re *Regexp) LiteralPrefix() (prefix string, complete bool) {
513 return re.prefix, re.prefixComplete
514 }
515
516
517
518 func (re *Regexp) MatchReader(r io.RuneReader) bool {
519 return re.doMatch(r, nil, "")
520 }
521
522
523
524 func (re *Regexp) MatchString(s string) bool {
525 return re.doMatch(nil, nil, s)
526 }
527
528
529
530 func (re *Regexp) Match(b []byte) bool {
531 return re.doMatch(nil, b, "")
532 }
533
534
535
536
537 func MatchReader(pattern string, r io.RuneReader) (matched bool, err error) {
538 re, err := Compile(pattern)
539 if err != nil {
540 return false, err
541 }
542 return re.MatchReader(r), nil
543 }
544
545
546
547
548 func MatchString(pattern string, s string) (matched bool, err error) {
549 re, err := Compile(pattern)
550 if err != nil {
551 return false, err
552 }
553 return re.MatchString(s), nil
554 }
555
556
557
558
559 func Match(pattern string, b []byte) (matched bool, err error) {
560 re, err := Compile(pattern)
561 if err != nil {
562 return false, err
563 }
564 return re.Match(b), nil
565 }
566
567
568
569
570 func (re *Regexp) ReplaceAllString(src, repl string) string {
571 n := 2
572 if strings.Contains(repl, "$") {
573 n = 2 * (re.numSubexp + 1)
574 }
575 b := re.replaceAll(nil, src, n, func(dst []byte, match []int) []byte {
576 return re.expand(dst, repl, nil, src, match)
577 })
578 return string(b)
579 }
580
581
582
583
584 func (re *Regexp) ReplaceAllLiteralString(src, repl string) string {
585 return string(re.replaceAll(nil, src, 2, func(dst []byte, match []int) []byte {
586 return append(dst, repl...)
587 }))
588 }
589
590
591
592
593
594 func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string {
595 b := re.replaceAll(nil, src, 2, func(dst []byte, match []int) []byte {
596 return append(dst, repl(src[match[0]:match[1]])...)
597 })
598 return string(b)
599 }
600
601 func (re *Regexp) replaceAll(bsrc []byte, src string, nmatch int, repl func(dst []byte, m []int) []byte) []byte {
602 lastMatchEnd := 0
603 searchPos := 0
604 var buf []byte
605 var endPos int
606 if bsrc != nil {
607 endPos = len(bsrc)
608 } else {
609 endPos = len(src)
610 }
611 if nmatch > re.prog.NumCap {
612 nmatch = re.prog.NumCap
613 }
614
615 var dstCap [2]int
616 for searchPos <= endPos {
617 a := re.doExecute(nil, bsrc, src, searchPos, nmatch, dstCap[:0])
618 if len(a) == 0 {
619 break
620 }
621
622
623 if bsrc != nil {
624 buf = append(buf, bsrc[lastMatchEnd:a[0]]...)
625 } else {
626 buf = append(buf, src[lastMatchEnd:a[0]]...)
627 }
628
629
630
631
632
633 if a[1] > lastMatchEnd || a[0] == 0 {
634 buf = repl(buf, a)
635 }
636 lastMatchEnd = a[1]
637
638
639 var width int
640 if bsrc != nil {
641 _, width = utf8.DecodeRune(bsrc[searchPos:])
642 } else {
643 _, width = utf8.DecodeRuneInString(src[searchPos:])
644 }
645 if searchPos+width > a[1] {
646 searchPos += width
647 } else if searchPos+1 > a[1] {
648
649
650 searchPos++
651 } else {
652 searchPos = a[1]
653 }
654 }
655
656
657 if bsrc != nil {
658 buf = append(buf, bsrc[lastMatchEnd:]...)
659 } else {
660 buf = append(buf, src[lastMatchEnd:]...)
661 }
662
663 return buf
664 }
665
666
667
668
669 func (re *Regexp) ReplaceAll(src, repl []byte) []byte {
670 n := 2
671 if bytes.IndexByte(repl, '$') >= 0 {
672 n = 2 * (re.numSubexp + 1)
673 }
674 srepl := ""
675 b := re.replaceAll(src, "", n, func(dst []byte, match []int) []byte {
676 if len(srepl) != len(repl) {
677 srepl = string(repl)
678 }
679 return re.expand(dst, srepl, src, "", match)
680 })
681 return b
682 }
683
684
685
686
687 func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte {
688 return re.replaceAll(src, "", 2, func(dst []byte, match []int) []byte {
689 return append(dst, repl...)
690 })
691 }
692
693
694
695
696
697 func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte {
698 return re.replaceAll(src, "", 2, func(dst []byte, match []int) []byte {
699 return append(dst, repl(src[match[0]:match[1]])...)
700 })
701 }
702
703
704 var specialBytes [16]byte
705
706
707 func special(b byte) bool {
708 return b < utf8.RuneSelf && specialBytes[b%16]&(1<<(b/16)) != 0
709 }
710
711 func init() {
712 for _, b := range []byte(`\.+*?()|[]{}^$`) {
713 specialBytes[b%16] |= 1 << (b / 16)
714 }
715 }
716
717
718
719
720 func QuoteMeta(s string) string {
721
722 var i int
723 for i = 0; i < len(s); i++ {
724 if special(s[i]) {
725 break
726 }
727 }
728
729 if i >= len(s) {
730 return s
731 }
732
733 b := make([]byte, 2*len(s)-i)
734 copy(b, s[:i])
735 j := i
736 for ; i < len(s); i++ {
737 if special(s[i]) {
738 b[j] = '\\'
739 j++
740 }
741 b[j] = s[i]
742 j++
743 }
744 return string(b[:j])
745 }
746
747
748
749
750
751
752 func (re *Regexp) pad(a []int) []int {
753 if a == nil {
754
755 return nil
756 }
757 n := (1 + re.numSubexp) * 2
758 for len(a) < n {
759 a = append(a, -1)
760 }
761 return a
762 }
763
764
765
766
767 func (re *Regexp) allMatches(s string, b []byte, n int, deliver func([]int)) {
768 var end int
769 if b == nil {
770 end = len(s)
771 } else {
772 end = len(b)
773 }
774
775 for pos, i, prevMatchEnd := 0, 0, -1; i < n && pos <= end; {
776 matches := re.doExecute(nil, b, s, pos, re.prog.NumCap, nil)
777 if len(matches) == 0 {
778 break
779 }
780
781 accept := true
782 if matches[1] == pos {
783
784 if matches[0] == prevMatchEnd {
785
786
787 accept = false
788 }
789 var width int
790
791 if b == nil {
792 _, width = utf8.DecodeRuneInString(s[pos:end])
793 } else {
794 _, width = utf8.DecodeRune(b[pos:end])
795 }
796 if width > 0 {
797 pos += width
798 } else {
799 pos = end + 1
800 }
801 } else {
802 pos = matches[1]
803 }
804 prevMatchEnd = matches[1]
805
806 if accept {
807 deliver(re.pad(matches))
808 i++
809 }
810 }
811 }
812
813
814
815 func (re *Regexp) Find(b []byte) []byte {
816 var dstCap [2]int
817 a := re.doExecute(nil, b, "", 0, 2, dstCap[:0])
818 if a == nil {
819 return nil
820 }
821 return b[a[0]:a[1]:a[1]]
822 }
823
824
825
826
827
828 func (re *Regexp) FindIndex(b []byte) (loc []int) {
829 a := re.doExecute(nil, b, "", 0, 2, nil)
830 if a == nil {
831 return nil
832 }
833 return a[0:2]
834 }
835
836
837
838
839
840
841 func (re *Regexp) FindString(s string) string {
842 var dstCap [2]int
843 a := re.doExecute(nil, nil, s, 0, 2, dstCap[:0])
844 if a == nil {
845 return ""
846 }
847 return s[a[0]:a[1]]
848 }
849
850
851
852
853
854 func (re *Regexp) FindStringIndex(s string) (loc []int) {
855 a := re.doExecute(nil, nil, s, 0, 2, nil)
856 if a == nil {
857 return nil
858 }
859 return a[0:2]
860 }
861
862
863
864
865
866
867 func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int) {
868 a := re.doExecute(r, nil, "", 0, 2, nil)
869 if a == nil {
870 return nil
871 }
872 return a[0:2]
873 }
874
875
876
877
878
879
880 func (re *Regexp) FindSubmatch(b []byte) [][]byte {
881 var dstCap [4]int
882 a := re.doExecute(nil, b, "", 0, re.prog.NumCap, dstCap[:0])
883 if a == nil {
884 return nil
885 }
886 ret := make([][]byte, 1+re.numSubexp)
887 for i := range ret {
888 if 2*i < len(a) && a[2*i] >= 0 {
889 ret[i] = b[a[2*i]:a[2*i+1]:a[2*i+1]]
890 }
891 }
892 return ret
893 }
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912 func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte {
913 return re.expand(dst, string(template), src, "", match)
914 }
915
916
917
918
919 func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte {
920 return re.expand(dst, template, nil, src, match)
921 }
922
923 func (re *Regexp) expand(dst []byte, template string, bsrc []byte, src string, match []int) []byte {
924 for len(template) > 0 {
925 i := strings.Index(template, "$")
926 if i < 0 {
927 break
928 }
929 dst = append(dst, template[:i]...)
930 template = template[i:]
931 if len(template) > 1 && template[1] == '$' {
932
933 dst = append(dst, '$')
934 template = template[2:]
935 continue
936 }
937 name, num, rest, ok := extract(template)
938 if !ok {
939
940 dst = append(dst, '$')
941 template = template[1:]
942 continue
943 }
944 template = rest
945 if num >= 0 {
946 if 2*num+1 < len(match) && match[2*num] >= 0 {
947 if bsrc != nil {
948 dst = append(dst, bsrc[match[2*num]:match[2*num+1]]...)
949 } else {
950 dst = append(dst, src[match[2*num]:match[2*num+1]]...)
951 }
952 }
953 } else {
954 for i, namei := range re.subexpNames {
955 if name == namei && 2*i+1 < len(match) && match[2*i] >= 0 {
956 if bsrc != nil {
957 dst = append(dst, bsrc[match[2*i]:match[2*i+1]]...)
958 } else {
959 dst = append(dst, src[match[2*i]:match[2*i+1]]...)
960 }
961 break
962 }
963 }
964 }
965 }
966 dst = append(dst, template...)
967 return dst
968 }
969
970
971
972 func extract(str string) (name string, num int, rest string, ok bool) {
973 if len(str) < 2 || str[0] != '$' {
974 return
975 }
976 brace := false
977 if str[1] == '{' {
978 brace = true
979 str = str[2:]
980 } else {
981 str = str[1:]
982 }
983 i := 0
984 for i < len(str) {
985 rune, size := utf8.DecodeRuneInString(str[i:])
986 if !unicode.IsLetter(rune) && !unicode.IsDigit(rune) && rune != '_' {
987 break
988 }
989 i += size
990 }
991 if i == 0 {
992
993 return
994 }
995 name = str[:i]
996 if brace {
997 if i >= len(str) || str[i] != '}' {
998
999 return
1000 }
1001 i++
1002 }
1003
1004
1005 num = 0
1006 for i := 0; i < len(name); i++ {
1007 if name[i] < '0' || '9' < name[i] || num >= 1e8 {
1008 num = -1
1009 break
1010 }
1011 num = num*10 + int(name[i]) - '0'
1012 }
1013
1014 if name[0] == '0' && len(name) > 1 {
1015 num = -1
1016 }
1017
1018 rest = str[i:]
1019 ok = true
1020 return
1021 }
1022
1023
1024
1025
1026
1027
1028 func (re *Regexp) FindSubmatchIndex(b []byte) []int {
1029 return re.pad(re.doExecute(nil, b, "", 0, re.prog.NumCap, nil))
1030 }
1031
1032
1033
1034
1035
1036
1037 func (re *Regexp) FindStringSubmatch(s string) []string {
1038 var dstCap [4]int
1039 a := re.doExecute(nil, nil, s, 0, re.prog.NumCap, dstCap[:0])
1040 if a == nil {
1041 return nil
1042 }
1043 ret := make([]string, 1+re.numSubexp)
1044 for i := range ret {
1045 if 2*i < len(a) && a[2*i] >= 0 {
1046 ret[i] = s[a[2*i]:a[2*i+1]]
1047 }
1048 }
1049 return ret
1050 }
1051
1052
1053
1054
1055
1056
1057 func (re *Regexp) FindStringSubmatchIndex(s string) []int {
1058 return re.pad(re.doExecute(nil, nil, s, 0, re.prog.NumCap, nil))
1059 }
1060
1061
1062
1063
1064
1065
1066 func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int {
1067 return re.pad(re.doExecute(r, nil, "", 0, re.prog.NumCap, nil))
1068 }
1069
1070 const startSize = 10
1071
1072
1073
1074
1075
1076 func (re *Regexp) FindAll(b []byte, n int) [][]byte {
1077 if n < 0 {
1078 n = len(b) + 1
1079 }
1080 var result [][]byte
1081 re.allMatches("", b, n, func(match []int) {
1082 if result == nil {
1083 result = make([][]byte, 0, startSize)
1084 }
1085 result = append(result, b[match[0]:match[1]:match[1]])
1086 })
1087 return result
1088 }
1089
1090
1091
1092
1093
1094 func (re *Regexp) FindAllIndex(b []byte, n int) [][]int {
1095 if n < 0 {
1096 n = len(b) + 1
1097 }
1098 var result [][]int
1099 re.allMatches("", b, n, func(match []int) {
1100 if result == nil {
1101 result = make([][]int, 0, startSize)
1102 }
1103 result = append(result, match[0:2])
1104 })
1105 return result
1106 }
1107
1108
1109
1110
1111
1112 func (re *Regexp) FindAllString(s string, n int) []string {
1113 if n < 0 {
1114 n = len(s) + 1
1115 }
1116 var result []string
1117 re.allMatches(s, nil, n, func(match []int) {
1118 if result == nil {
1119 result = make([]string, 0, startSize)
1120 }
1121 result = append(result, s[match[0]:match[1]])
1122 })
1123 return result
1124 }
1125
1126
1127
1128
1129
1130 func (re *Regexp) FindAllStringIndex(s string, n int) [][]int {
1131 if n < 0 {
1132 n = len(s) + 1
1133 }
1134 var result [][]int
1135 re.allMatches(s, nil, n, func(match []int) {
1136 if result == nil {
1137 result = make([][]int, 0, startSize)
1138 }
1139 result = append(result, match[0:2])
1140 })
1141 return result
1142 }
1143
1144
1145
1146
1147
1148 func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte {
1149 if n < 0 {
1150 n = len(b) + 1
1151 }
1152 var result [][][]byte
1153 re.allMatches("", b, n, func(match []int) {
1154 if result == nil {
1155 result = make([][][]byte, 0, startSize)
1156 }
1157 slice := make([][]byte, len(match)/2)
1158 for j := range slice {
1159 if match[2*j] >= 0 {
1160 slice[j] = b[match[2*j]:match[2*j+1]:match[2*j+1]]
1161 }
1162 }
1163 result = append(result, slice)
1164 })
1165 return result
1166 }
1167
1168
1169
1170
1171
1172 func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int {
1173 if n < 0 {
1174 n = len(b) + 1
1175 }
1176 var result [][]int
1177 re.allMatches("", b, n, func(match []int) {
1178 if result == nil {
1179 result = make([][]int, 0, startSize)
1180 }
1181 result = append(result, match)
1182 })
1183 return result
1184 }
1185
1186
1187
1188
1189
1190 func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string {
1191 if n < 0 {
1192 n = len(s) + 1
1193 }
1194 var result [][]string
1195 re.allMatches(s, nil, n, func(match []int) {
1196 if result == nil {
1197 result = make([][]string, 0, startSize)
1198 }
1199 slice := make([]string, len(match)/2)
1200 for j := range slice {
1201 if match[2*j] >= 0 {
1202 slice[j] = s[match[2*j]:match[2*j+1]]
1203 }
1204 }
1205 result = append(result, slice)
1206 })
1207 return result
1208 }
1209
1210
1211
1212
1213
1214
1215 func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int {
1216 if n < 0 {
1217 n = len(s) + 1
1218 }
1219 var result [][]int
1220 re.allMatches(s, nil, n, func(match []int) {
1221 if result == nil {
1222 result = make([][]int, 0, startSize)
1223 }
1224 result = append(result, match)
1225 })
1226 return result
1227 }
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244 func (re *Regexp) Split(s string, n int) []string {
1245
1246 if n == 0 {
1247 return nil
1248 }
1249
1250 if len(re.expr) > 0 && len(s) == 0 {
1251 return []string{""}
1252 }
1253
1254 matches := re.FindAllStringIndex(s, n)
1255 strings := make([]string, 0, len(matches))
1256
1257 beg := 0
1258 end := 0
1259 for _, match := range matches {
1260 if n > 0 && len(strings) >= n-1 {
1261 break
1262 }
1263
1264 end = match[0]
1265 if match[1] != 0 {
1266 strings = append(strings, s[beg:end])
1267 }
1268 beg = match[1]
1269 }
1270
1271 if end != len(s) {
1272 strings = append(strings, s[beg:])
1273 }
1274
1275 return strings
1276 }
1277
View as plain text