mgo正则表达式不起作用

时间:2022-06-11 21:45:00

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\.

现在我有一些文档,每个文档都有一个键路径和值,如\ 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.

我想找到只有1段的那些。这意味着结果应该是\ A \和\ B \。我使用正则表达式/ ^ \\ [^ \\] * \\ $ /,它在MongoDB终端中工作正常。但是,当我尝试将它应用于Go程序时,它不起作用。

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?

这是mgo的错误吗?

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

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

3 个解决方案

#1


5  

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).

在Go中,反斜杠(\)是解释的字符串文字的转义字符(使用“...”作为附件)。在您的情况下,您宁愿使用原始字符串文字(使用`...`作为附件)。

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

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

#2


0  

Simply use this

只需使用它

   wordOffset := anyregular expression OR text to filter

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

#3


0  

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

要添加到@ sandun-priyanka解决方案,如果要使其不区分大小写:

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

#1


5  

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).

在Go中,反斜杠(\)是解释的字符串文字的转义字符(使用“...”作为附件)。在您的情况下,您宁愿使用原始字符串文字(使用`...`作为附件)。

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

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

#2


0  

Simply use this

只需使用它

   wordOffset := anyregular expression OR text to filter

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

#3


0  

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

要添加到@ sandun-priyanka解决方案,如果要使其不区分大小写:

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