1

If we run this example with go 1.16

re := regexp.MustCompile(`(?:^|_)(.)`)
fmt.Printf("%s", re.FindAll([]byte("i_love_golang_and_json_so_much"), -1))

the output is:

[i _l _g _a _j _s _m]

However, if we run it in Python3.6

import re
print(re.findall('(?:^|_)(.)', 'i_love_golang_and_json_so_much'))

the output is:

['i', 'l', 'g', 'a', 'j', 's', 'm']

The output of go has a leading _ before each match group.

Since ?: is for non capturing group, it seems the result from Python is more reasonable?

Why the result from go carries a leading _?

Is there a way to get the same output as Python(without leading _) with go?

buxizhizhoum
  • 1,719
  • 1
  • 23
  • 32
  • 3
    FindAll returns the *matches* not the captured *submatches*. Use FindAllSubmatch and extract the submatches to get the same result as python. https://go.dev/play/p/mGA8YIc3GKO – mkopriva Nov 26 '21 at 06:41
  • 2
    The return value for [Python's findall](https://docs.python.org/3/library/re.html#re.findall) depends on the number of capturing groups in the pattern. If there is is exactly one group in the pattern, then findall returns that group. The Go FindAll returns the entire match. Use FindAllSubmatch per previous comment. – Charlie Tumahai Nov 26 '21 at 06:44
  • 1
    For a start (and for a better question), make the strings you're matching the same. – Ulrich Eckhardt Nov 26 '21 at 07:05
  • @mkopriva It works, thanks a lot! – buxizhizhoum Nov 26 '21 at 07:14
  • @Cerise Limón, Your comment makes it more clear, thank you very much! – buxizhizhoum Nov 26 '21 at 07:16

0 Answers0