8

I want to give users a text area where they can enter text. Later I will match that input against a different input and extract it if matched.

Flow:

  • User enters text with returns into a text area
  • Text is saved in the db in a text field

Then I use the following to extract:

text_reply = text_reply.sub(/#{user.text_to_extract}/m, '').strip

Problem is that it appears that characters like new lines or pipes | are breaking it. As input that we want to match against can look like this:

XXXXXX

XXXXXX
XXXX & XXXXX
asdasd: 123312321 | dasasddsadasads

http://yahoo.com

Suggestions? Thansk

1
  • A simple string replace won't do? Commented Mar 22, 2011 at 19:08

1 Answer 1

15

You need to escape the input: http://www.ruby-doc.org/core/classes/Regexp.html

See the method escape.

 Regexp.escape(your_input)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but how do I escape when I do inputtext.split(regex_to_use).first ?
You do the same thing to that regex if you are taking in user/string input for the regex. Use Regex.escape for any string that may have regex special characters that you do not want to be interpreted as so by the regex engine.
I don't think this is the issue. It appears to be a | pipe in the input that break s it.
Yes which is exactly what escaping it avoids. A pipe is a special character in regexes
@AnApprentice: Since you discovered the error here, you can select this as the correct answer and close out this question.

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.