Regular Expression doesn't work.

690 views
Skip to first unread message

Shane Hou

unread,
Apr 10, 2014, 12:51:34 PM4/10/14
to mgo-...@googlegroups.com
Now I have some documents like:

{"_id": "blahblahblah1", "path": "\\A\\"} (mongo terminal prints "\\", but actually it's "\")
{"_id": "blahblahblah2", "path": "\\B\\"}
{"_id": "blahblahblah3", "path": "\\A\\C\\"}
{"_id": "blahblahblah4", "path": "\\A\\C\\D\\"}
{"_id": "blahblahblah5", "path": "\\A\\E\\"}
{"_id": "blahblahblah6", "path": "\\A\\E\\F\\"}

 And I want to find \\A\\ and \\B\\.

I used c.Find(bson.M{"path": bson.M{"$regex": bson.RegEx{"^\\[^\\]*\\$", ""}}}), but it doesn't work. The most weired part is, this regular expression can work in the mongo terminal.

And I also found that everything with "\\" cannot work.
So is it a bug of mgo?

Shane Hou

unread,
Apr 10, 2014, 1:25:37 PM4/10/14
to mgo-...@googlegroups.com
I don't know if it's confusing.
About the documents, each of the documents has a key path and value like \A\, \B\, \A\C\, \A\C\D\, \A\E\, \A\E\F\.
And what I want are the documents with only 1 segment, like \A\ and \B\.
I use /^\\[^\\]*\\$/ in mongo terminal and it works fine. But when it comes to mgo, it didn't get correct result.

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)

It outputs:
[]
instead of [{"\\A\\"}, {"\\B\\"}] (some other fields are omitted).

And I also found even err = c.Find(bson.M{"path": bson.M{"$regex": bson.RegEx{"\\", ""}}}).All(&nodeList) would produce []

So is it a bug?

Shane Hou

unread,
Apr 10, 2014, 10:59:46 PM4/10/14
to mgo-...@googlegroups.com
All right, although nobody answers this, I got my answer on StackOverflow.
What I need to do is to use \\ instead of \ to construct the regex. That means I should use "\\\\" or `\\`.
But why? I stored my data with only 1 \, why do I need to find it with double \?

Norberto Leite

unread,
Apr 11, 2014, 7:19:51 AM4/11/14
to mgo-...@googlegroups.com
Hi Shane, 

if you store a string that uses backslash like "\something" you need to escape that so the regular expression does not confuse it with the back slash meaning (http://misc.yarinareth.net/regex.html). When parsing that in go you also need to escape it and therefore backshlash it once more.
So what you are trying to do is 
db.col.find(  {fields: { $regex: "^\\..."} }) //escaping the char '\' => "\\"
translates into go like:
c.Find(bson.M{"field": bson.M{"$regex": bson.RegEx{"^\\\\.....", ""}}}) // escaping the escape and the backslash.

N.



  


--
You received this message because you are subscribed to the Google Groups "mgo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


{ "name"     : "Norberto Leite",

  "title"    : "SA | Eng",

  "phone"    : "0034.687.954.987",

  "location" : ["Barcelona, ES", "London, UK"],

  "twitter"  : ["@MongoDB""@MongoDBInc", "@nleite"],

  "facebook" : ["MongoDB", "MongoDB, Inc."] }

Gustavo Niemeyer

unread,
Apr 11, 2014, 1:08:56 PM4/11/14
to mgo-...@googlegroups.com
The backslash is a common escape character in many languages,
including Go. That's how you get \n to become the byte 0x0A, for
example.

You can avoid having to escape the slash in Go by using a raw string
literal, which is enclosed in back quotes (`) rather than double
quotes. For example:

http://play.golang.org/p/KBAC-e6oWp
> --
> You received this message because you are subscribed to the Google Groups
> "mgo-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mgo-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

gustavo @ http://niemeyer.net
Reply all
Reply to author
Forward
0 new messages