2

I have a piece of code like this:

Foo.where("name ~* '.*#{string}.*'")

Where string has both word characters and non-word characters and if some of them are not escaped I get invalid Regular Expressions errors.

So I need to get postgreslq to take string as a literal or escape the meta-characters used by the regex. Is there a way to do it instead of getting a list of all meta-characters and adding the backslash?


I have looked at both these questions but none of the answers is exactly what I'm looking for:

Here the accepted answer escapes all non-word characters. And using that with my strings I am afraid there will be weird behaviors with escaped non-word characters.

Here the answer is specific to the question and can't be used in my case.

1

2 Answers 2

2

I think you can just use Ruby's Regexp.escape() to espace characters in string that have a special meaning in a regular expression.

For example

> string = 'C++ /?|foo bar baz'
=> "C++ /?|"
> Regexp.escape(string)
=> "C\\+\\+\\ /\\?\\|foo\\ bar\\ baz"

With this in mind you would write

Foo.where("name ~* '.*#{Regexp.escape(string)}.*'")

Note: this is untested.

Sign up to request clarification or add additional context in comments.

Comments

1

You could just plainly convert all characters of string to the ASCII or Unicode hex number and use the \xhh or \x{hhhh}. Should easy to code and should not have side-effects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.