Home > OS >  Issue with regex in Golang
Issue with regex in Golang

Time:01-07

I am trying to create a regex to parse specific string .

The current string is abcd_1.263.0.15-8zz00df.yml and I want to parse only 1.263.0.15-8zz00df out of it.

Tried already with this expression "_\K.*(?=\.)" but its not working in Golan and giving me pattern error. Can someone please help with this?

CodePudding user response:

Go uses RE2 regex engine, that does not support lookaheads, lookbehinds and other PCRE goodies like \K

See this comparison of the different regex engines.

You could however use this regex:

[^_-] -[^.] 

See this demo.

Explained:

[^_-]    # a charter that is not "_" or "-", one or more times
-        # a literal "-"
[^.]     # a character that is not a dot, one or more times

CodePudding user response:

Edit: You can simply use the regular expression:

_(.*)\.

The * matches greedily, which means that it will match everything until the last '.' - this is exactly what you need. Your match is in group 1.


Why are you using the \K matcher? Your regular expression works like this:

_(.*)(?=\.)

and group 1 contains your match.

Note: a very helpful tool to test regular expressions is this site: https://regexr.com/

CodePudding user response:

Just reposting one of @mkopriva snippet with a sentence,

not everything needs to be done with regular expressions :

    s := "abcd_1.263.0.15-8zz00df.yml"

    if i := strings.IndexByte(s, '_'); i > -1 {
        s = s[i 1:]
    }
    if i := strings.LastIndexByte(s, '.'); i > -1 {
        s = s[:i]
    }

    fmt.Println(s)

playground

  •  Tags:  
  • Related