mgo正则表达式不起作用。

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

mgo regular expression doesn't work

问题

现在我有一些文档,每个文档都有一个键path和值,如\A\\B\\A\C\\A\C\D\\A\E\\A\E\F\

我想找出只有一个段的文档。也就是说,结果应该是\A\\B\。我在MongoDB终端中使用了正则表达式/^\\[^\\]*\\$/,在那里运行得很好。但是当我尝试将其应用到Go程序中时,它不起作用。

Go代码:

var nodeList []NodeEntry // NodeEntry将匹配一个文档的每个字段
err = c.Find(bson.M{"path": bson.M{"$regex": bson.RegEx{"^\\[^\\]*\$", ""}}}).
    All(&nodeList)
fmt.Println(nodeList)

输出:

[]

这太奇怪了,然后我发现任何带有\\的正则表达式都会产生空结果。

所以这是mgo的一个错误吗?

(我不知道这是否不合适,但我也在mgo.users邮件列表上发布了这个问题。)

英文:

Now I have some documents, each of which has a key path and value like \A\, \B\, \A\C\, \A\C\D\, \A\E\, \A\E\F\.

I want to find the ones which have only 1 segment. It means the result should be \A\ and \B\. I use Regular Expression /^\\[^\\]*\\$/, which works fine in MongoDB terminal. But when I tried to apply it to Go programs it doesn't work.

Go codes:

var nodeList []NodeEntry // NodeEntry would match every field of one document
err = c.Find(bson.M{"path": bson.M{"$regex": bson.RegEx{"^\\[^\\]*\$", ""}}}).All(&nodeList)
fmt.Println(nodeList)

Output:

[]

It's so strange, and then I found out that any Regex with \\ would produce an empty result.

So is it a bug of mgo?

(I don't know if it's inappropriate, but I've also posted this question on the mgo.users mailing list.)

答案1

得分: 6

在Go语言中,反斜杠(\)是解释字符串字面值(使用"..."作为封闭符号)的转义字符。在你的情况下,你更应该使用原始字符串字面值(使用`...`作为封闭符号)。

让我们看一下这段代码:

package main

import "fmt"

func main() {
    fmt.Println(`^\[^\\]*\$`)
    fmt.Println(`^\\[^\\]*\$`)
}

结果:

^\[^\]*$
^\\[^\\]*\$

你可以看到第二个选项才是你想要的正则表达式字符串。所以,为了解决你的问题,只需将你的正则表达式字符串用反引号括起来,而不是用引号:

err = c.Find(bson.M{"path": bson.M{"$regex": bson.RegEx{`^\\[^\\]*\$`, ""}}}).
    All(&nodeList)

Go规范参考: http://golang.org/ref/spec#String_literals

英文:

In Go, the backslash (\) is the escape character of an interpreted string literal (using "..." as enclosures). In your case, you´d rather want to use a raw string literal (using `...` as enclosures).

Let's look at this piece of code:

package main

import "fmt"

func main() {
	fmt.Println("^\\[^\\]*\$")
	fmt.Println(`^\\[^\\]*\$`)
}

Result:

^\[^\]*$
^\\[^\\]*\$

You can see that it is the second option that is the regex string you desire. So, to solve your problem, just enclose your regex string in backticks instead of quotes:

err = c.Find(bson.M{"path": bson.M{"$regex": bson.RegEx{`^\\[^\\]*\$`, ""}}}).All(&nodeList)

Go spec reference: http://golang.org/ref/spec#String_literals

答案2

得分: 1

要添加到@sandun-priyanka的解决方案中,如果你想要不区分大小写:

selector:= bson.M{"title": bson.M{"$regex": `(?i)`+wordOffset}}
英文:

To add to @sandun-priyanka solution, if you want to make it case insensitive:

selector:= bson.M{"title": bson.M{"$regex": `(?i)`+wordOffset}}

答案3

得分: 0

只需使用以下代码:

wordOffset := 任何正则表达式或文本筛选条件

selector := bson.M{"title": bson.M{"$regex": wordOffset}}

这段代码用于在MongoDB中使用正则表达式或文本筛选条件来过滤标题字段。

英文:

Simply use this

   wordOffset := anyregular expression OR text to filter
    
   selector:= bson.M{"title": bson.M{"$regex": wordOffset}}

huangapple
  • 本文由 发表于 2014年4月11日 01:15:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/22994727.html
匿名

发表评论

匿名网友

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

确定