用户输入 + Ruby + 正则表达式? [英] User Input + Ruby + Regex?
本文介绍了用户输入 + Ruby + 正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为用户提供一个文本区域,他们可以在其中输入文本.稍后我会将该输入与不同的输入进行匹配,并在匹配时提取它.
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.
流程:
- 用户在文本区域输入文本并返回
- 文本保存在数据库中的文本字段
然后我使用以下内容提取:
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
建议?谢谢
推荐答案
您需要对输入进行转义:http://www.ruby-doc.org/core/classes/Regexp.html
You need to escape the input: http://www.ruby-doc.org/core/classes/Regexp.html
见方法escape
.
Regexp.escape(your_input)
这篇关于用户输入 + Ruby + 正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文