我使用regexp.Compile(`^123(?:4)`)来尝试从”1234abcd”中获取”123″,但结果是”1234″。

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

I use regexp.Compile(`^123(?:4)`) to try to get "123" from "1234abcd" while it came out to be "1234"

问题

我正在使用Go 1.8.3进行编码,并且我想使用regexp包从"1234abcd"中获取"123"。当我使用regexp.Compile("^123(?:4)")时,它返回了"1234"。

以下是你的代码的中文翻译:

package main

import (
	"regexp"
	"fmt"
)

func main() {
	test, err := regexp.Compile(`^123(?:4)`)
	if err != nil {
		fmt.Println(err)
		return
	}
	input := "1234|wserw"
	fmt.Println(test.FindString(input))
}

输出结果为:1234

期望结果为:123

英文:

I'm coding with go 1.8.3 and I want to use regexp package to get "123" from "1234abcd". When I used regexp.Compile("^123(?:4)"), it came to "1234".

Coding in this: https://play.golang.org/p/jB7FmxWz9r

package main

import (
	"regexp"
	"fmt"
)

func main() {
	test, err := regexp.Compile(`^123(?:4)`)
	if err != nil {
		fmt.Println(err)
		return
	}
	input := "1234|wserw"
	fmt.Println(test.FindString(input))
}

came out: 1234

expected: 123

答案1

得分: 2

根据https://groups.google.com/forum/#!topic/golang-nuts/8Xmvar_ptcU的说法:

捕获组是指在索引匹配列表中记录的()部分,其他语言可能称之为$1、$2、$3等等。非捕获组是一种使用()但不占用这些数字的方式。无论组是否捕获,都不会对整个表达式匹配的完整字符串产生影响。在这里匹配的完整字符串是"datid=12345",因此FindString返回的就是这个字符串。

你使用非捕获组的原因与你在算术表达式中使用括号(x+y)*z的原因相同:覆盖默认的运算符优先级。无论是否使用组,优先级都是相同的。

换句话说,(?:datid=)[0-9]{5}与datid=[0-9]{5}是完全相同的正则表达式。

因此,这种行为是golang开发者有意为之的。

解决方法是使用regexp.Compile(`^(123)(?:4)`),并使用FindStringSubmatch来捕获它。

英文:

according to https://groups.google.com/forum/#!topic/golang-nuts/8Xmvar_ptcU

> A capturing group is a ( ) that is recorded in the indexed match list, what other languages might call $1, $2, $3 and so on. A non-capturing group is a way to use ( ) without taking one of those numbers. Whether a group is capturing or not has no effect on the full string matched by the overall expression. The full string matched here is "datid=12345", and so that is what FindString returns.
>
> You use non-capturing groups for the same reason you use parentheses in the arithmetic expression (x+y)*z: overriding the default operator precedence. The precedence is the same here with or without the group.
>
> Put another way, (?:datid=)[0-9]{5} is exactly the same regular expression as datid=[0-9]{5}.

so that behavior is intended by golang's developer.

the workaround is using regexp.Compile(`^(123)(?:4)`), and capture it using FindStringSubmatch

huangapple
  • 本文由 发表于 2017年7月27日 14:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/45342488.html
匿名

发表评论

匿名网友

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

确定