Split input string in go by regex

golang string split by comma
golang regex
golang regex groups
golang regex extract
golang regex multi-line
golang regex playground
golang regex escape
golang string split by space

How can I split input strings below by regex in Go? Examples of strings:

I know how to split by dot, but how can I avoid splitting in quotes?

"a.b.c.d" -> ["a", "b", "c", "d"]
"a."b.c".d" -> ["a", "b.c", "d"]
"a.'b.c'.d" -> ["a", "b.c", "d"]

Here is another option with a somewhat less hacky regex. It uses the trash bin trick. So the real data is on the (first and second) capturing groups.

It works even with nested quotes like this: "a.'b.c'.d.e."f.g.h"" as long as there is not a recursion of 2 or more levels (as in here: "a.'b."c.d"'", quotes inside quotes inside quotes).

The regex is this: ^"|['"](\w+(?:\.\w+)*)['"]|(\w+)

And the code:

package main

import (
    "regexp"
    "fmt"
)

func main() {
    var re = regexp.MustCompile(`^"|['"](\w+(?:\.\w+)*)['"]|(\w+)`)
    var str = `"a.'b.c'.d.e."f.g.h""`

    result := re.FindAllStringSubmatch(str, -1)
    for _, m := range result {
        if (m[1] != "" || m[2] != "") {
            fmt.Print(m[1] + m[2] + "\n")
        }
    }
}

Input:

"a.'b.c'.d.e."f.g.h""

Output:

a
b.c
d
e
f.g.h

3 ways to split a string into a slice · YourBasic Go, Split on regular expression​​ It splits a string into substrings separated by a regular expression. The method takes an integer argument n ; if n >= 0 , it returns at most n substrings. See this Regexp tutorial and cheat sheet for a gentle introduction to the Go regexp package with plenty of examples. Split (String, Int32) Splits an input string a specified maximum number of times into an array of substrings, at the positions defined by a regular expression specified in the Regex constructor. Split (String) Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.


How to Split a String in Golang?, Split: This function splits a string into all substrings separated by the given separator and returns a slice which contains these substrings. Syntax: func Split(​str, sep  With the Regex class, the delimiters are found using a regular expression. The basic way to call Split is using two string parameters . The first contains the input string and the second holds the regular expression to match.


Matching balanced delimiters is a complex problem for regular expressions, as evidenced by John's answer. Unless you're using something like the Go pcre package.

Instead the Go CSV parser can be adapted. Configure it to use . as the separator, lazy quotes (the CSV quote is '), and variable length records.

package main

import (
    "encoding/csv"
    "fmt"
    "io"
    "log"
    "strings"
)

func main() {
    lines := `a.b.c.d
a.\"b.c\".d
a.'b.c'.d
`

    csv := csv.NewReader(strings.NewReader(lines))
    csv.Comma = '.'
    csv.LazyQuotes = true
    csv.FieldsPerRecord = -1
    for {
        record, err := csv.Read()
        if err == io.EOF {
            break
        }
        if err != nil {
            log.Fatal(err)
        }

        fmt.Printf("%#v\n", record)
    }
}

Golang, If 'String' is present, the argument is a string; otherwise it is a slice of bytes; repl func(string) string) string: func (re *Regexp) Split(s string, n int) []string: func (re regexp returns a match that begins as early as possible in the input (leftmost),  In more complicated situations, the regexp Split method might do the trick. It splits a string into substrings separated by a regular expression. The method takes an integer argument n ; if n >= 0, it returns at most n substrings.


regexp, Args , or parse some line based input like lines from an IRC chat, As it happens Go has the excellent strings package in the standard Split(str[end+1:index[0]-1]​, " ") newParts = append(newParts, words. I join the slices back into a full string, before I perform a regex match for the quoted strings inside. We use Regex.Split to split on all non-digit values in the input string. We then loop through the result strings, with a foreach-loop, and use int.TryParse.int.Parse. Input: The input string contains the numbers 10, 20, 40 and 1, and the static Regex.Split method is called with two parameters.


Parsing strings with Go, Split a string at uppercase letters using regular expression in Golang. package main import ( "fmt" "regexp" ) func main() { str1 := "Hello X42 I'm a Y-32.35 string  You don't need to use regular expressions if you just want to split a string by multiple spaces. Writing your own regex library is overkill for something that simple. The answer you linked to in your comments, Split a string in C++?, can easily be changed so that it doesn't include any empty elements if there are multiple spaces.


Split a string at uppercase letters using regular expression in Golang , The Fields function breaks a string around each instance of one or more consecutive white space How to extract text from between html tag using Regular Expressions in Golang? The HTML form have 2 input box of name and address. func (re *Regexp) Split(s string, n int) []string Split slices s into substrings separated by the expression and returns a slice of the substrings between those expression matches. The slice returned by this method consists of all the substrings of s not contained in the slice returned by FindAllString.