Home > Enterprise >  python regex split function equivalent in golang
python regex split function equivalent in golang

Time:12-30

I've below python code to match regex::

import re
digits_re = re.compile("([0-9eE. ]*)")

p = digits_re.split("Hello, where are you 1.1?")

print(p)

It gives this output::   ['', '', 'H', 'e', '', '', 'l', '', 'l', '', 'o', '', ',', '', ' ', '', 'w', '', 'h', 'e', '', '', 'r', 'e', '', '', ' ', '', 'a', '', 'r', 'e', '', '', ' ', '', 'y', '', 'o', '', 'u', '', ' ', '1.1', '', '', '?', '', '']

I'm trying to get the above output with golang.

package main

import (
    "bytes"
    "fmt"
    "log"
    "os/exec"
    "regexp"
    "strconv"
)

func main() {
    // Execute the command and get the output
    cmd := exec.Command("echo", "Hello, where are you 1.1?")
    var out bytes.Buffer
    cmd.Stdout = &out
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }

    // Extract the numerical values from the output using a regular expression
    re := regexp.MustCompile(`([0-9eE. ]*)`)
    //matches := re.FindAllString(out.String(), -1)
    splits := re.Split(out.String(), -1)
    fmt.Println(splits)

}

I get output like below::

[H l l o ,   w h r   a r   y o u   ? 
]

I think regex is language dependant, so split() function used in python won't be helpful with golang. Having used multiple Find*() functions part of regexp package but couldn't find one which could provide the output of the above python program.

The goal of the output string array is to separate characters that can't be converted to float & if a string can be parsed to float, I calculate the moving average.

In the end, I combine everything & present output like Linux watch command.

Shall you need more details/context? I would be glad to share.

Your help is much appreciated!

CodePudding user response:

In the meantime, I got something which works for my use case. It's not exactly in the same format as python but without empty strings/chars.

I used both Split & FindAllString functions to get desired output.

    // unfortunately go regex Split doesn't work like python
    // so we construct the entire array with matched string (numericals)
    // and the split strings to combine the output
    splits := digitsRe.Split(string(out), -1)
    matches := digitsRe.FindAllString(string(out), -1)

    p := []string{}
    for i := 0; i < len(splits); i   {
        p = append(p, matches[i])
        p = append(p, splits[i])
    }

With above snippet, I get something like ::

[H e l l o ,   w h e r e   a r e   y o u  1.1 ?]

I'm not that good with regex, somehow the above one works for my use case.

On a lighter note, I heard a joke from my colleague that if you are solving a problem with the help of regex, you will end up with two problems!!!

CodePudding user response:

To match the given output, pls see if this helps you.

import re
p = re.sub(r"[0-9eE. ]", "", "Hello, where are you 1.1?")
p = [p] 
# p = list(p) ## based on your requirement
print(p)
  • Related