沿正则表达式分割字符串,但保持匹配

我想按常规表达式拆分字符串,但保留匹配项。</ p>

我曾尝试在正则表达式上分割字符串,但会丢掉匹配项。 我也尝试使用,但是我不太擅长翻译代码 </ p>

  re:= regexp.MustCompile(\ d
array:= re .Split(“ ab1cd2ef3”,-1)
</ code> </ pre>

我需要将数组的值设置为[“ ab”,“ 1”,“ cd”,“ 2 “,” ef“,” 3“],但数组的值为[” ab“,” cd“,” ef“]。 没有错误。</ p>
</ div>

展开原文

原文

I want to split a string on a regular expresion, but preserve the matches.

I have tried splitting the string on a regex, but it throws away the matches. I have also tried using this, but I am not very good at translating code from language to language, let alone C#.

re := regexp.MustCompile(`\d`)
array := re.Split("ab1cd2ef3", -1)

I need the value of array to be ["ab", "1", "cd", "2", "ef", "3"], but the value of array is ["ab", "cd", "ef"]. No errors.

3个回答



Go regex软件包中不提供您所指出的链接中对regex的支持。 您可以阅读相关讨论。 </ p>

要实现的目标(根据给定的示例)可以使用正则表达式来匹配数字或非数字。</ p>

  包main 

import(
“ fmt”
“ regexp”

func main(){
str:=“ ab1cd2ef3”
r:= regexp.MustCompile((\ d | [ ^ \ d] +)
fmt.Println(r.FindAllStringSubmatch(str,-1))
}
</ code> </ pre>

游乐场: https://play.golang.org/p/L-ElvkDky53 </ p>
\ n

输出:</ p>

  [[ab ab] [1 1] [cd cd] [2 2] [ef ef] [3 3]] 
</ 代码> </ pre>
</ div>

展开原文

原文

The kind of regex support in the link you have pointed out is NOT available in Go regex package. You can read the related discussion.

What you want to achieve (as per the sample given) can be done using regex to match digits or non-digits.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    str := "ab1cd2ef3"
    r := regexp.MustCompile(`(\d|[^\d]+)`)
    fmt.Println(r.FindAllStringSubmatch(str, -1))
}

Playground: https://play.golang.org/p/L-ElvkDky53

Output:

[[ab ab] [1 1] [cd cd] [2 2] [ef ef] [3 3]]

doupao3662
doupao3662 它只是用于匹配令牌的正则表达式。 因此,只要您可以使用正则表达式分别表示令牌,就可以使用|分隔多个令牌。 如果可以提供一些示例字符串,则将更易于理解。
大约一年之前 回复
duanchuanqu593743
duanchuanqu593743 我可以对多个分隔符执行相同的操作吗? 好像我不能。 (\(\)| [^ \(\)] +)
大约一年之前 回复

I don't think this is possible with the current regexp package, but the Split could be easily extended to such behavior.

This should work for your case:

func Split(re *regexp.Regexp, s string, n int) []string {
    if n == 0 {
        return nil
    }

    matches := re.FindAllStringIndex(s, n)
    strings := make([]string, 0, len(matches))

    beg := 0
    end := 0
    for _, match := range matches {
        if n > 0 && len(strings) >= n-1 {
            break
        }

        end = match[0]
        if match[1] != 0 {
            strings = append(strings, s[beg:end])
        }
        beg = match[1]
        // This also appends the current match
        strings = append(strings, s[match[0]:match[1]])
    }

    if end != len(s) {
        strings = append(strings, s[beg:])
    }

    return strings
}



哑巴解决方案。 在字符串中添加分隔符并使用分隔符进行分隔。</ p>

 包main 

import(
“ fmt”
“ regexp”
“ strings”
)\ n
func main(){
re:= regexp.MustCompile(\ d +
输入:=“ ab1cd2ef3”
sep:=“ |”

索引:= re.FindAllStringIndex(input, -1)
fmt.Println(indexes)

对于_,v:=范围索引{
p1:= v [0] + move
p2:= v [1] + move
输入=输入[:p1] + sep +输入[p1:p2] + sep +输入[p2:]
移动+ = 2
}

结果:= string.Split(input, sep)

fmt.Println(result)
}
</ code> </ pre>
</ div>

展开原文

原文

Dumb solutions. Add separator in the string and split with separator.

package main

import (
    "fmt"
    "regexp"
    "strings"
)

func main() {
    re := regexp.MustCompile(`\d+`)
    input := "ab1cd2ef3"
    sep := "|"

    indexes := re.FindAllStringIndex(input, -1)
    fmt.Println(indexes)

    move := 0
    for _, v := range indexes {
        p1 := v[0] + move
        p2 := v[1] + move
        input = input[:p1] + sep + input[p1:p2] + sep + input[p2:]
        move += 2
    }

    result := strings.Split(input, sep)

    fmt.Println(result)
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问