I have a set of special characters for Elasticsearch that I need to escape with Ruby.
我有一组特殊的字符用于弹框搜索,我需要用Ruby来逃避。
They are: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
它们是:+ - = & || > < !(){ }[]”^ ~ * ?:\ /
How can I get any string to escape any of these characters?
我怎么能得到任何字符串来摆脱这些字符呢?
Thanks
谢谢
2 个解决方案
#1
3
The problem as it is stated has no solution, because “escaping two subsequent characters” makes no sense. What result do you expect to receive “escaping”, say, &&
?
正如所述的问题没有解决办法,因为“转义两个后续字符”没有意义。你期望得到什么结果?
I believe, you want to escape all single characters, so that &&
becomes \&\&
and ||
— \|\|
. That is easy.
我相信,您希望转义所有的单个字符,使&&变成\&\& || - \|\|。这是很容易的。
to_escape = %w_+ - = & | > < ! ( ) { } [ ] ^ " ~ * ? : \ /_ # C"mon, SO parser
re = Regexp.union(to_escape)
print 'str (f) | a || b'.gsub(re) { |m| "\\#{m}" }
#⇒ str \(f\) \| a \|\| b
Another possibility would be to use Regexp#escape
, but it will escape more, than you probably need (e. g. spaces.)
另一种可能是使用Regexp#escape,但是它会比您可能需要的转义更多(例如空格)。
#2
2
This is a variation on @mudasobwa's answer, using the form of String#gsub that uses a hash for replacements:
这是@mudasobwa的答案的变体,使用字符串#gsub的形式,使用散列替换:
escapees = %w$ + - = & | > < ! ( ) { } [ ] ^ " ~ * ? : \ / $
#=> ["+", "-", "=", "&", "|", ">", "<", "!", "(", ")", "{", "}",
# "[", "]", "^", "\"", "~", "*", "?", ":", " /"]
h = escapees.each_with_object({}) { |c,h| h[c] = "\\#{c}" }
#=> {"+"=>"\\+", "-"=>"\\-",..., " /"=>"\\ /"}
h.default_proc = ->(h,k) { k }
If the hash h
does not have a key k
, Hash#default_proc= causes the h[k]
to to return k
.
如果哈希h没有键k,哈希#default_proc=导致h[k]返回k。
s = 'str (f) | a || b'
ss = s.gsub(/./,h)
#=> "str \\(f\\) \\| a \\|\\| b"
puts ss
#=> str \(f\) \| a \|\| b
#1
3
The problem as it is stated has no solution, because “escaping two subsequent characters” makes no sense. What result do you expect to receive “escaping”, say, &&
?
正如所述的问题没有解决办法,因为“转义两个后续字符”没有意义。你期望得到什么结果?
I believe, you want to escape all single characters, so that &&
becomes \&\&
and ||
— \|\|
. That is easy.
我相信,您希望转义所有的单个字符,使&&变成\&\& || - \|\|。这是很容易的。
to_escape = %w_+ - = & | > < ! ( ) { } [ ] ^ " ~ * ? : \ /_ # C"mon, SO parser
re = Regexp.union(to_escape)
print 'str (f) | a || b'.gsub(re) { |m| "\\#{m}" }
#⇒ str \(f\) \| a \|\| b
Another possibility would be to use Regexp#escape
, but it will escape more, than you probably need (e. g. spaces.)
另一种可能是使用Regexp#escape,但是它会比您可能需要的转义更多(例如空格)。
#2
2
This is a variation on @mudasobwa's answer, using the form of String#gsub that uses a hash for replacements:
这是@mudasobwa的答案的变体,使用字符串#gsub的形式,使用散列替换:
escapees = %w$ + - = & | > < ! ( ) { } [ ] ^ " ~ * ? : \ / $
#=> ["+", "-", "=", "&", "|", ">", "<", "!", "(", ")", "{", "}",
# "[", "]", "^", "\"", "~", "*", "?", ":", " /"]
h = escapees.each_with_object({}) { |c,h| h[c] = "\\#{c}" }
#=> {"+"=>"\\+", "-"=>"\\-",..., " /"=>"\\ /"}
h.default_proc = ->(h,k) { k }
If the hash h
does not have a key k
, Hash#default_proc= causes the h[k]
to to return k
.
如果哈希h没有键k,哈希#default_proc=导致h[k]返回k。
s = 'str (f) | a || b'
ss = s.gsub(/./,h)
#=> "str \\(f\\) \\| a \\|\\| b"
puts ss
#=> str \(f\) \| a \|\| b