Re: Search string for occurneces of words stored in array



David A. Black [2008-04-30 16:29]:
Jens Wille [2008-04-30 16:18]:
sentence =~ Regexp.union(*words)
one addition regarding the regexp, though. in case words may
contain special characters, it's safer to escape them first:

sentence =~ Regexp.union(*words.map { |word| Regexp.escape(word) })
It actually does it for you:

Regexp.union("a",".b") => /a|\.b/
ha, didn't know that ;-) thank you!

.



Relevant Pages

  • Re: Search string for occurneces of words stored in array
    ... one addition regarding the regexp, ... special characters, it's safer to escape them first: ...
    (comp.lang.ruby)
  • Re: How to include a string into a regex comparison?
    ... When you insert a string inside a regexp, it's evaluated as is by the ... regexp engine. ... You need to escape the special characters, ...
    (comp.lang.ruby)
  • Re: query string
    ... some special characters cannot be passed on ... the query string unless I escape them... ... that redirects to another page passing the value ... > escaped using the function escape of javascript. ...
    (microsoft.public.dotnet.framework.aspnet)
  • query string
    ... My question derives from the fact that, because I'm using portuguese keyboard layout and language, some special characters cannot be passed on the query string unless I escape them... ... on the onclick event of the button I've an instruction that redirects to another page passing the value ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: sed command fails if line has quote
    ... I might have forgotten to escape certain ... You have a regexp: ... A systemcommand: ... But than you have a script that depends on external commands and so it ...
    (comp.lang.php)