0

I have several strings that include various symbols like the following two examples:

z=y+x

@symbol

and I want to split the strings such that I have the resulting slices:

[z = y + x]

[@ symbol]

A few things I've looked at and tried:

  1. I've looked at this question but it seems as though golang doesn't support lookarounds.

  2. I know this solution exists using strings.SplitAfter, but I'm looking to have the delimiters as separate elements.

  3. I tried replacing the symbol (e.g. "+") with some variant (e.g. "~+~") and doing a split on the surrounding characters (e.g. "~"), but this solution is far from elegant and runs into problems if I need to do a conditional replacement depending on the symbol (which golang doesn't seem to support either).

Perhaps I've misunderstood some of the previous question and their respective solutions.

jimmy
  • 109
  • 2
  • 9

1 Answers1

1

I used a modified version of Go's strings.Split implementation https://golang.org/src/strings/strings.go?s=7505:7539#L245

func Test(te *testing.T) {
    t := tester.New(te)
    t.Assert().Equal(splitCharsInclusive("z=y+x", "=+"), []string{"z", "=", "y", "+", "x"})
    t.Assert().Equal(splitCharsInclusive("@symbol", "@"), []string{"", "@", "symbol"})

}

func splitCharsInclusive(s, chars string) (out []string) {
    for {
        m := strings.IndexAny(s, chars)
        if m < 0 {
            break
        }
        out = append(out, s[:m], s[m:m+1])
        s = s[m+1:]
    }
    out = append(out, s)
    return
}

This is limited to single characters to split on. And passing something like splitCharsInclusive("(z)(y)(x)", "()") might not get you the output you want, as you'd get a few empty strings in the response. But hopefully this is a good starting point for the modifications you need.

Also, Go's version that I've linked calculates the length of the output array in advance, this is a nice optimization that I've decided to omit, but would likely be good to add back.

maxm
  • 3,412
  • 1
  • 19
  • 27