我可以确认一下该字符串是否与我的正则表达式的第一个块匹配吗?

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

Can I confirm if the string is matching with the first block at my regex

问题

我有以下代码:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := "This is a delimited string, so let's go"
	re := regexp.MustCompile(`(?i)(This is a delimited string)|delimited|string`)
	matches := re.FindAllString(str, -1)
	fmt.Println("We found", len(matches), "matches, that are:", matches)
}

并且得到以下输出:

We found 1 matches, that are: [This is a delimited string]

如果我将上述代码中的 str 更改为:

str := "This is not a delimited string, so let's go"

那么我得到的输出是:

We found 2 matches, that are: [delimited string]

两者都是正确的,但在第一个代码块中,str 匹配了我的正则表达式中的第一个代码块,即 This is a delimited string,而第二个代码块中的 str 显示了 2 个匹配项,但没有一个与我的正则表达式中的第一个代码块匹配。

是否有一种方法可以知道 str 是否与我的正则表达式中的第一个代码块匹配,以便我可以获得完全匹配或部分匹配,即使 len(matches) 不为零,但正则表达式中的第一个代码块没有匹配!

英文:

I've the below code:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := "This is a delimited string, so let's go"
	re := regexp.MustCompile(`(?i)(This is a delimited string)|delimited|string`)
	matches := re.FindAllString(str, -1)
	fmt.Println("We found", len(matches), "matches, that are:", matches)
}

And getting the output as:

We found 1 matches, that are: [This is a delimited string]

If I changed the str in the above code to be:

str := "This is not a delimited string, so let's go"

Then I'm getting the output as:

We found 2 matches, that are: [delimited string]

Both are correct, but in the first block str which is having 1 match is matching 100% with the first block at my regex, which is This is a delimited string, while str in the second block showing 2 matches, none of them is matching with my first block at my regex.

Is there a way, so I know if the str is matching with the first block at my regex or no, so that I get complete matchorpartial matchis thelen(matches)` is not zero, but the first block at the regex is not matched!

答案1

得分: 2

这个正则表达式:

(?i)(This is a delimited string)|delimited|string

匹配这些字面字符串的最左最长匹配:

  • This is a delimited string
  • delimited
  • string

将文本This is a delimited string, so let's go输入到正则表达式中,第一个选择项匹配。这是唯一的匹配,因为它消耗了delimitedstring,搜索继续在匹配之后的位置进行。

将你的替代文本This is not a delimited string, so let's go输入到正则表达式中会得到2个匹配,因为第一个选择项不匹配,但第二个(delimited)和第三个(string)匹配。

如果你想知道哪个选择项匹配了,只需将每个选择项用括号括起来,使其成为一个捕获组:

(?i)(This is a delimited string)|(delimited)|(string)

现在我们可以检查每个捕获组的值:如果长度大于1,则表示匹配了该选择项。

https://goplay.tools/snippet/nTm56_al__2

package main

import (
	"fmt"
	"regexp"
)

func main() {
	str := "This is a delimited string, so let's go and find some delimited strings"

	re := regexp.MustCompile(`(?i)(This is a delimited string)|(delimited)|(string)`)

	matches := re.FindAllStringSubmatch(str, -1)

	fmt.Println("Matches:", len(matches))
	for i, match := range matches {
		j := 1
		for j < len(match) && match[j] == "" {
			j++
		}
		fmt.Println("Match", i, "matched alternative", j, "with", match[j])
	}

}
英文:

This regular expression:

(?i)(This is a delimited string)|delimited|string

matches the leftmost longest match of these literal strings:

  • This is a delimited string,
  • delimited, or
  • string

Feeding the text This is a delimited string, so let&#39;s go to the regular expression, the 1st alternative matches. It is the only match, because it consumed both delimited and string — the search continues at the point following the match.

Feeding your alternate text, This is not a delimited string, so let&#39;s go to the regular expression results in 2 matches, because the 1st alternative does not match, but the 2nd (delimited) and the 3rd (string) do.

If you want to know which alternative matched, simply enclose each alternative in parentheses to make it a capturing group:

(?i)(This is a delimited string)|(delimited)|(string)`

Now we can check the value of each capturing group: if it has a length greater than 1, it's the alternative that matched.

https://goplay.tools/snippet/nTm56_al__2

package main

import (
	&quot;fmt&quot;
	&quot;regexp&quot;
)

func main() {
	str := &quot;This is a delimited string, so let&#39;s go and find some delimited strings&quot;

	re := regexp.MustCompile(`(?i)(This is a delimited string)|(delimited)|(string)`)

	matches := re.FindAllStringSubmatch(str, -1)

	fmt.Println(&quot;Matches:&quot;, len(matches))
	for i, match := range matches {
		j := 1
		for j &lt; len(match) &amp;&amp; match[j] == &quot;&quot; {
			j++
		}
		fmt.Println(&quot;Match&quot;, i, &quot;matched alternative&quot;, j, &quot;with&quot;, match[j])
	}

}

huangapple
  • 本文由 发表于 2022年8月30日 22:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/73543841.html
匿名

发表评论

匿名网友

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

确定