Protect regular expression

18 views
Skip to first unread message

nz

unread,
Feb 16, 2015, 2:16:50 PM2/16/15
to mgo-...@googlegroups.com
Hi

Lets say I have a string variable "term" from and I used it in bson.RegEx{Pattern:string(`^.*` + term + `.*`)}  is there a way to encode variable term so it does not interfere with the rest of the regular expression ? 

thanks
Nz

Gustavo Niemeyer

unread,
Feb 24, 2015, 1:15:52 PM2/24/15
to mgo-...@googlegroups.com
Hi nz,

There's no built in regular expression escaping right now, so you'll
need to study the rules and escape them yourself for the time being.
It's not a bad idea to have that as a standard feature, though.
> --
> 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

nz

unread,
Feb 24, 2015, 1:47:26 PM2/24/15
to mgo-...@googlegroups.com


On Tuesday, 24 February 2015 10:15:52 UTC-8, Gustavo Niemeyer wrote:
Hi nz,

There's no built in regular expression escaping right now, so you'll
need to study the rules and escape them yourself for the time being.
It's not a bad idea to have that as a standard feature, though.

Something quick I whipped together, looks like it should work to replace characters

re := regexp.MustCompile(`([-[\]{}()*+?.,\\^$|#\s])`)
fmt.Println(re.ReplaceAllString("foo.*", "\\$1"))
 

Reply all
Reply to author
Forward
0 new messages