String substitution without RegEx

A

Andreas Schwarz

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.

Andreas
 
T

ts

A> I wanted to do a simple string substitution, and was surprised to see
A> that there isn't such a method in ruby. I can't use String::(g)sub,
A> because the substitution mustn't be influenced by special chars or
A> backreferences in the pattern and replacement strings. Of course I could
A> write functions to escape regex special chars in the pattern and
A> backreferences in the replacement string, but this is about the ugliest
A> 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('\\*?{}.') #=> \\\\\*\?\{\}\.

svg%


Guy Decoux
 
A

Andreas Schwarz

ts said:
A> I wanted to do a simple string substitution, and was surprised to see
A> that there isn't such a method in ruby. I can't use String::(g)sub,
A> because the substitution mustn't be influenced by special chars or
A> backreferences in the pattern and replacement strings. Of course I could
A> write functions to escape regex special chars in the pattern and
A> backreferences in the replacement string, but this is about the ugliest
A> 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.
 
M

Martin Weber

A> (...) Of course I could
A> write functions to escape regex special chars in the pattern and
A> backreferences in the replacement string, but this is about the ugliest
A> solution I can think of.

Well Regexp#escape don't do what you want ?

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
 
T

ts

A> 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.

Perhaps with Regexp#escape

svg% ruby -e 'p "[]{}".gsub(/#{Regexp.escape("]{")}/, Regexp.escape("\\1"))'
"[\\1}"
svg%


Guy Decoux
 
M

Mike Stok

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.

[mike@ratdog mike]$ ri Regexp::escape
This is a test 'ri'. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

--------------------------------------------------------- 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('\\*?{}.') #=> \\\\\*\?\{\}\.

e.g.

[mike@ratdog mike]$ irb --simple-prompt=> "Oh dear!"

Hope this helps,

Mike
 
D

Dave Thomas

Andreas said:
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"
 
A

Andreas Schwarz

Dave said:
irb(main):006:0> a = "dave"
=> "dave"
irb(main):007:0> a["av"] = "onat"
=> "onat"
irb(main):008:0> a
=> "donate"

Thanks, now I'm using the following methods:

class String

def simple_sub!(find, replace)
self[find] = replace
return true
rescue
return false
end

def simple_gsub!(find, replace)
c = 0
while self.simple_sub!(find, replace)
c += 1
end
return c
end

end

end
 
R

Robert Klemme

Andreas Schwarz said:
irb(main):001:0> "foo.bar".gsub '.', "*"
=> "*******"

Ah, then it's a ruby version thingy. I used 1.7.3. Which one do you
have?
irb(main):007:0> "foo.bar".gsub( /#{Regexp.escape(".")}/, "\\1")
=> "foobar"

Why do you use \1 if you want to replace a fixed string? Or do you want
to insert \1? Then you shoul've done

"foo.bar".gsub( /#{Regexp.escape(".")}/, Regexp.escape("\\1") )

Cheers

robert
 
A

Andreas Schwarz

Robert said:
Ah, then it's a ruby version thingy. I used 1.7.3. Which one do you
have?

ruby 1.6.8
Why do you use \1 if you want to replace a fixed string? Or do you want
to insert \1?
Yes.

Then you shoul've done

"foo.bar".gsub( /#{Regexp.escape(".")}/, Regexp.escape("\\1") )

irb(main):004:0> "foo.bar".gsub( /#{Regexp.escape(".")}/, Regexp.escape(".\\1") )
returns
"foo\\.\\1bar"
instead of
"foo.\\1bar"

Is there really no easy way to replace strings in Ruby?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top