英文:
python regex split function equivalent in golang
问题
我有以下的Python代码来匹配正则表达式:
import re
digits_re = re.compile("([0-9eE.+]*)")
p = digits_re.split("Hello, where are you 1.1?")
print(p)
它给出以下输出:
['', '', 'H', 'e', '', '', 'l', '', 'l', '', 'o', '', ',', '', ' ', '', 'w', '', 'h', 'e', '', '', 'r', 'e', '', '', ' ', '', 'a', '', 'r', 'e', '', '', ' ', '', 'y', '', 'o', '', 'u', '', ' ', '', '1.1', '', '', '?', '', '']
我正在尝试使用golang
来获得上述输出。
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
)
func main() {
// 执行命令并获取输出
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)
}
// 使用正则表达式从输出中提取数值
re := regexp.MustCompile(`([0-9eE.+]*)`)
//matches := re.FindAllString(out.String(), -1)
splits := re.Split(out.String(), -1)
fmt.Println(splits)
}
我得到以下输出:
[H l l o , w h r a r y o u ?
]
我认为正则表达式是与语言相关的,所以在golang
中使用split()
函数不会像在Python中那样有帮助。我尝试使用regexp
包中的多个Find*()
函数,但没有找到一个可以提供上述Python程序输出的函数。
输出字符串数组的目标是将无法转换为float
的字符分开,如果一个字符串可以解析为浮点数,我会计算移动平均值。
最后,我将所有内容组合起来,并像Linux的watch
命令一样呈现输出。
如果需要更多的细节/上下文,我很乐意分享。
非常感谢你的帮助!
英文:
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!
答案1
得分: 0
与此同时,我找到了适用于我的用例的解决方案。它的格式不完全与python
相同,但没有空字符串/字符。
我使用了Split
和FindAllString
函数来获得所需的输出。
// 不幸的是,go的正则表达式Split函数不像python那样工作
// 所以我们用匹配的字符串(数字)和分割字符串构建整个数组,以组合输出
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])
}
使用上述代码片段,我得到了类似于::
[H e l l o , w h e r e a r e y o u 1.1 ?]
我对regex
不是很擅长,但不知何故上述代码对我的用例有效。
轻松一点说,我从同事那里听到一个笑话,如果你用regex
解决一个问题,你最终会遇到两个问题!!!
英文:
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!!!
答案2
得分: -1
为了匹配给定的输出,请看看这是否对你有帮助。
import re
p = re.sub(r"[0-9eE.+]", "", "Hello, where are you 1.1?")
p =
# p = list(p) ## 根据你的要求
print(p)
英文:
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 = list(p) ## based on your requirement
print(p)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论