Golang正则表达式用于匹配字符直到一个字符串(不包括子字符串)。

huangapple go评论86阅读模式
英文:

Golang regExp for matching characters till a string ( excluding substrings)

问题

我可以帮你编写相应的 Golang 正则表达式。以下是符合你要求的正则表达式:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	input := "abpcdcccddd"
	pattern := `a.*?ccc`
	re := regexp.MustCompile(pattern)
	match := re.FindString(input)
	fmt.Println(match)
}

这个正则表达式会匹配以字母"a"开头,以"ccc"结尾的字符串。在你提供的示例中,它会匹配到"abpcdccc"。注意,这里使用了非贪婪模式(.*?),以确保匹配的字符串尽可能短。

英文:

I have a use case: Need to match the following .
Input string: abpcdcccddd
Matching criteria: match all characters starting with 'a' and ending with 'ccc'
Example: abpcdccc ( single character 'c' in 4th Poistion got successfully ignored in matching)

Can you help me with the Golang regular expression for the same?

答案1

得分: 1

我不确定我完全理解你想要的是什么,但我可能可以帮你找到正确的方法。

如果你想匹配一个字符串,其中包含一个 a,后面跟着一些单词字符,然后是 ccc,你可以简单地使用这样的表达式:

a\w+ccc

如果你想让字符串以 a 开头,以 ccc 结尾,你可以这样做:

^a\w+ccc$

如果你只想允许特定的字符,比如小写字母,你可以将它们放入字符类中,像这样:

a[a-z]+ccc

希望其中一个可以回答你的问题。

英文:

I'm not sure that I understand exactly what it is you are wanting, but I can probably get you on the right track.

If you want to match a string that has an a, followed by some word characters, followed by ccc, then you can simply use something like this:

a\w+ccc

If you want the string to start and end with a and ccc respectively, you can do something like this:

^a\w+ccc$

If you want to only allow specific characters like lowercase letters, you can put them into a character class like this:

a[a-z]+ccc

Hopefully one of those answers your question.

答案2

得分: 0

这完全取决于你对“所有字符”这个词的确切理解...它可以包括或不包括字符集,如空格(可选包括换行符)、字母、数字、符号、Unicode集等。

所以...

如果你真的是指包括第一个"a"和"ccc"之间的所有字符(包括空格),你需要使用:

a.+ccc

如果你只是指所有的“单词字符”,你可以使用以下之一:

a\w+ccc
a[0-9a-zA-Z_]+ccc

所有字母:

a[a-zA-Z]+ccc

所有小写字母:

a[a-z]+ccc

...根据你对该定义的实际要求,还会有更多选项。

这里有一个小程序来测试其中的一些情况:

package main

import "fmt"
import "regexp"

func main() {
        res := []string{
                `a.+ccc`,
                `a\w+ccc`,
                `a[a-z]+ccc`,
        }
        ss := []string{
                "ablablacccc",
                "adadasdccc",
                "sadlkasdlkcccc",
                "accc",
                "blaafoewoiccc",
                "oirorwccc",
                "abla ablaccc",
        }
        for _, re := range res {
                r := regexp.MustCompile(re)
                fmt.Printf("正则表达式:%q\n", re)
                for _, s := range ss {
                        fmt.Printf("    情况 %q:匹配=%v 匹配结果=%q\n", s, r.MatchString(s), r.FindString(s))
                }
        }
}

它产生的输出如下:

正则表达式:"a.+ccc"
    情况 "ablablacccc":匹配=true 匹配结果="ablablacccc"
    情况 "adadasdccc":匹配=true 匹配结果="adadasdccc"
    情况 "sadlkasdlkcccc":匹配=true 匹配结果="adlkasdlkcccc"
    情况 "accc":匹配=false 匹配结果=""
    情况 "blaafoewoiccc":匹配=true 匹配结果="aafoewoiccc"
    情况 "oirorwccc":匹配=false 匹配结果=""
    情况 "abla ablaccc":匹配=true 匹配结果="abla ablaccc"
正则表达式:"a\\w+ccc"
    情况 "ablablacccc":匹配=true 匹配结果="ablablacccc"
    情况 "adadasdccc":匹配=true 匹配结果="adadasdccc"
    情况 "sadlkasdlkcccc":匹配=true 匹配结果="adlkasdlkcccc"
    情况 "accc":匹配=false 匹配结果=""
    情况 "blaafoewoiccc":匹配=true 匹配结果="aafoewoiccc"
    情况 "oirorwccc":匹配=false 匹配结果=""
    情况 "abla ablaccc":匹配=true 匹配结果="ablaccc"
正则表达式:"a[a-z]+ccc"
    情况 "ablablacccc":匹配=true 匹配结果="ablablacccc"
    情况 "adadasdccc":匹配=true 匹配结果="adadasdccc"
    情况 "sadlkasdlkcccc":匹配=true 匹配结果="adlkasdlkcccc"
    情况 "accc":匹配=false 匹配结果=""
    情况 "blaafoewoiccc":匹配=true 匹配结果="aafoewoiccc"
    情况 "oirorwccc":匹配=false 匹配结果=""
    情况 "abla ablaccc":匹配=true 匹配结果="ablaccc"
英文:

It all depends on what do you exactly mean with "all characters"... it could include or not character sets such as whitespace (optionally including newlines), letters, numbers, symbols, unicode sets, etc.

So...

If you really meant "all characters" (i.e. including whitespace) between the first "a" and "ccc" you need to use:

a.+ccc

If you meant just all "word characters" you can use one of these:

a\w+ccc
a[0-9a-zA-Z_]+ccc

All letters:

a[a-zA-Z]+ccc

All lowcase letters:

a[a-z]+ccc

...and there will be more options depending on your real requirements for that definition.

Here you have a small program to test some of that cases:

package main

import "fmt"
import "regexp"

func main() {
        res := []string{
                `a.+ccc`,
                `a\w+ccc`,
                `a[a-z]+ccc`,
        }
        ss := []string{
                "ablablacccc",
                "adadasdccc",
                "sadlkasdlkcccc",
                "accc",
                "blaafoewoiccc",
                "oirorwccc",
                "abla ablaccc",
        }
        for _, re := range res {
                r := regexp.MustCompile(re)
                fmt.Printf("regular expression: %q\n", re)
                for _, s := range ss {
                        fmt.Printf("    case %q: matches=%v match=%q\n", s, r.MatchString(s), r.FindString(s))
                }
        }
}

And the output it produces:

regular expression: "a.+ccc"
    case "ablablacccc": matches=true match="ablablacccc"
    case "adadasdccc": matches=true match="adadasdccc"
    case "sadlkasdlkcccc": matches=true match="adlkasdlkcccc"
    case "accc": matches=false match=""
    case "blaafoewoiccc": matches=true match="aafoewoiccc"
    case "oirorwccc": matches=false match=""
    case "abla ablaccc": matches=true match="abla ablaccc"
regular expression: "a\\w+ccc"
    case "ablablacccc": matches=true match="ablablacccc"
    case "adadasdccc": matches=true match="adadasdccc"
    case "sadlkasdlkcccc": matches=true match="adlkasdlkcccc"
    case "accc": matches=false match=""
    case "blaafoewoiccc": matches=true match="aafoewoiccc"
    case "oirorwccc": matches=false match=""
    case "abla ablaccc": matches=true match="ablaccc"
regular expression: "a[a-z]+ccc"
    case "ablablacccc": matches=true match="ablablacccc"
    case "adadasdccc": matches=true match="adadasdccc"
    case "sadlkasdlkcccc": matches=true match="adlkasdlkcccc"
    case "accc": matches=false match=""
    case "blaafoewoiccc": matches=true match="aafoewoiccc"
    case "oirorwccc": matches=false match=""
    case "abla ablaccc": matches=true match="ablaccc"

huangapple
  • 本文由 发表于 2017年1月5日 00:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/41468968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定