Home > Enterprise >  golang extract string between two pipes
golang extract string between two pipes

Time:08-31

I have a string like so:

Accommodation | XYZ | ABC

I need to extract the string between the pipes, just 'XYZ'

How do I do this in Go/ RE2?

CodePudding user response:

I would recommend using regular expressions since it's still lightweight execution after compilation.

It goes like this:

import (
    "fmt"
    "regexp"
)

str := "Accommodation | XYZ | ABC"
r := regexp.MustCompile(`\|\s(.*?)\s\|`)

list := r.FindStringSubmatch(str)

for i := 1; i < len(list); i   {
    fmt.Println(list[i])
}
  • Related