正则表达式 – 如何匹配模式出现 X 次后的子字符串?

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

regex - How to match a substring after X occurrences of a pattern?

问题

在下面的DNS条目中,我试图匹配h6部分(位置4)。在这一点上,我知道该域的这一部分只由2个字母/数字或其中之一组成,所以我可以使用以下方式(笨拙地)进行匹配:

"-[a-zA-Z0-9]{2}-"

在无法假设这是唯一一个由2个字母/数字组成的域的情况下,如何仅匹配第4个位置的内容减去-ab1是第一个位置,cde23是第二个位置,以此类推,所有位置都由-分隔)

我能够使用以下正则表达式匹配到第4个位置,但它包括从开头开始的所有内容。

"([a-zA-Z0-9]*-){3}[a-zA-Z0-9]*-"

我在golang中使用这些正则表达式。

英文:
ab1-cde23-fg45-h6-ijk-789.lmn.local.        86400 IN A  12.34.5.123

In the follow DNS entry, I'm trying to match the h6 section (position 4). At this point, I know this section of the domain is only composed of 2 letters/digits or one of each, so I can match it (in a clumsy way) with

"-[a-zA-Z0-9]{2}-"

In a case where I could not assume that this is the only section of a domain with 2 letters/digits, how could I match only the content of the 4th position minus the -? (ab1 being the first position, cde23 the second, and so on, with all the positions separated by -)

I'm able to match up to the 4th positions with the following regex, but it includes everything from the start.

"([a-zA-Z0-9]*-){3}[a-zA-Z0-9]*-"

I'm using theses regexp in golang.

答案1

得分: 6

做:

^(?:[^-]+-){3}([^-]+)
  • ^(?:[^-]+-){3} 匹配以 - 分隔的前三个字段,(?:) 使该组不捕获

  • 被捕获的组 ([^-]+) 将包含以 - 分隔的第四个字段。

演示


在这个问题上,也许你应该考虑使用字符串操作而不是昂贵的正则表达式实现,简单的 strings.Split() 应该可以解决问题:

package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "ab1-cde23-fg45-h6-ijk-789.lmn.local.        86400 IN A  12.34.5.123"
	fmt.Println(strings.Split(s, "-")[3])
}
英文:

Do:

^(?:[^-]+-){3}([^-]+)
  • ^(?:[^-]+-){3} matches - separated first 3 fields, (?:) makes the group non-capturing

  • The captured group, ([^-]+) will contain the - separated 4th field.

Demo


While we are at this, you should perhaps look at string manipulation rather than costly Regex implementation, plain strings.Split() should do:

package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "ab1-cde23-fg45-h6-ijk-789.lmn.local.        86400 IN A  12.34.5.123"
	fmt.Println(strings.Split(s, "-")[3])
}

huangapple
  • 本文由 发表于 2017年1月31日 12:53:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/41949880.html
匿名

发表评论

匿名网友

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

确定