英文:
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.
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])
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论