I wanted to do a simple string substitution, and was surprised to see
that there isn’t such a method in ruby. I can’t use String::(g)sub,
because the substitution mustn’t be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
I wanted to do a simple string substitution, and was surprised to see
that there isn't such a method in ruby. I can't use String::(g)sub,
because the substitution mustn't be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
Well Regexp#escape don't do what you want ?
svg% ri Regexp#escape
--------------------------------------------------------- Regexp::escape
Regexp.escape( aString ) -> aNewString
···
------------------------------------------------------------------------
Escapes any characters that would have special meaning in a regular
expression. For any string, Regexp.escape(str)=~str will be true.
Regexp.escape('\\*?{}.') #=> \\\\\*\?\{\}\.
I wanted to do a simple string substitution, and was surprised to see
that there isn’t such a method in ruby. I can’t use String::(g)sub,
because the substitution mustn’t be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
You can use Regexp.escape rather than write one yourself.
Escapes any characters that would have special meaning in a regular
expression. For any string, Regexp.escape(str)=~str will
be true.
Regexp.escape('\\*?{}.') #=> \\\\\*\?\{\}\.
I wanted to do a simple string substitution, and was surprised to see
that there isn’t such a method in ruby. I can’t use String::(g)sub,
because the substitution mustn’t be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
There’s a subliminate message here…
irb(main):006:0> a = “dave”
=> “dave”
irb(main):007:0> a[“av”] = “onat”
=> “onat”
irb(main):008:0> a
=> “donate”
I wanted to do a simple string substitution, and was surprised to see
that there isn’t such a method in ruby. I can’t use String::(g)sub,
because the substitution mustn’t be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
Where’s the problem?
irb(main):004:0> “foo.bar”.gsub ‘.’, “"
(irb):4: warning: string pattern instead of regexp; metacharacters no
longer eff
ective
"foobar”
irb(main):005:0>
There’s just this ugly warnig. Anyone know a way to switch that off?
Or you use Regexp.escape as others already have pointed out:
I wanted to do a simple string substitution, and was surprised to see
that there isn’t such a method in ruby. I can’t use String::(g)sub,
because the substitution mustn’t be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
It might help him, but I can understand how he feels… everytime I write
a pattern as a regexp which in the end just is a plain string, or a pattern
which would do with glob-style matching, I have the impression I’m throwing
cannons after pigeons again (nothing against your comp, guy
-martin
···
On Wed, Jul 23, 2003 at 01:38:08AM +0900, ts wrote:
(…) Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
I wanted to do a simple string substitution, and was surprised to see
that there isn’t such a method in ruby. I can’t use String::(g)sub,
because the substitution mustn’t be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
Well Regexp#escape don’t do what you want ?
I would still need a function to remove backreferences from the
replacement string. It’s possible, of course, but it hurts me that I
have to use such “hacks” for a simple substitution.
I wanted to do a simple string substitution, and was surprised to see
that there isn’t such a method in ruby. I can’t use String::(g)sub,
because the substitution mustn’t be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the ugliest
solution I can think of.
Where’s the problem?
irb(main):004:0> “foo.bar”.gsub ‘.’, “"
(irb):4: warning: string pattern instead of regexp; metacharacters no
longer eff
ective
"foobar”
I would still need a function to remove backreferences from the
replacement string. It's possible, of course, but it hurts me that I
have to use such "hacks" for a simple substitution.
I wanted to do a simple string substitution, and was surprised to see
that there isn’t such a method in ruby. I can’t use String::(g)sub,
because the substitution mustn’t be influenced by special chars or
backreferences in the pattern and replacement strings. Of course I
could
write functions to escape regex special chars in the pattern and
backreferences in the replacement string, but this is about the
ugliest
solution I can think of.
Where’s the problem?
irb(main):004:0> “foo.bar”.gsub ‘.’, “"
(irb):4: warning: string pattern instead of regexp; metacharacters no
longer eff
ective
"foobar”
I would still need a function to remove backreferences from the A>
replacement string. It’s possible, of course, but it hurts me that I A>
have to use such “hacks” for a simple substitution.
And then, so you don’t have to type that all every time, just make it a
method of String. Being able to do this is one of the best features of
Ruby. =)
class String
def simple_sub(from,to)
sub(/#{Regexp.escape(from)}/,Regexp.escape(to))
end
end