views:

33

answers:

3

Hi!

I'm trying to store regexes in a database but they're getting escaped by rails.

For example \w*\s\/\s becomes \\w*\\s\\/\\s in the database and when retrieved.

I'm inserting trying to use them with mystring.sub(/#{regex_variable}/, ''), but the escaped regex is not matching as desired.

What's the best we to resolve this so the regex works as input?

Thanks!

A: 

I don't know ruby syntax but you could do a global replace of \\ for \

Crayon Violent
+1  A: 

I think what Crayon means in Ruby is:

mystring.gsub("\\\\", "\\")

I think I escaped them correctly.

Sorry if I misunderstood the question.

EDIT: For that question, refer to this question.

Jorge Israel Peña
A: 

Thanks for the suggestions. As it turns out the Rails console escapes the backslashes on output (why?). That also explains why when I'd tried the gsub that Blaenk suggested, it didn't appear to be working (I had previously tried that myslef to no avail).

So my question now is why the regex works when I put it directly i quotes, but not in mystring.sub(/#{regex_variable}/, '') form, but I guess I need to open a new quoestion for that.

MattB
Please take a look at my answer, I edited it.
Jorge Israel Peña