I want to search one pattern in a file line by line and replace it with
new pattern one. But it doesn't work. I cannot change the old pattern
into the new one using gsub! Is there any problem with my script?
I want to search one pattern in a file line by line and replace it with
new pattern one. But it doesn't work. I cannot change the old pattern
into the new one using gsub! Is there any problem with my script?
Regexp.escape takes a string as a parameter, and returns a string as its
return value. When you use that string in your Regexp.escape call, you
convert it into something that no longer matches the text in your file,
and when you use the returned string in the gsub call, it tries to match
it literally.
On Mon, 11 Jun 2007 22:34:16 +0900, Li Chen wrote:
Hi all,
I want to search one pattern in a file line by line and replace it with
new pattern one. But it doesn't work. I cannot change the old pattern
into the new one using gsub! Is there any problem with my script?
DATA.each_line do |line|
if line.match(r1_obj)
line.gsub!(r1_obj, r2_obj) #
puts line
end
end
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/
When you put a backslash in a string, it is eaten up by the interpreter
BEFORE ANY METHODS ARE ACTUALLY CALLED. Regexp#escape gets the string
AFTER backslashes have been eaten.
So there's nothing there for it to escape. The escape method isn't
magic; it can't tell how your source is laid out, only what actual string
it is given.
-s
···
In message <b4c60e38ff75eea451c544b6d2d7a2a2@ruby-forum.com>, Li Chen writes:
Thanks, but the replacement still doesn't work.
It is my understanding that Regexp#escape method should do the magic
work for me.
This is because the 2nd string can use \1, \2, etc... as a replacement
for a previous match in the 1st string (matches are specified in the
1st string in parentheses). This topic was discussed here a few days
ago:
# In single quoted strings you only escape \ and '
# You can interpolate into a pattern literal.
pat = /#{Regexp.escape('1.000000\$P1G\1.000000')}/
# No need for Regexp.escape here since the second parameter to gsub!
# is a string, not a pattern.
sub = '2.000000\\$P1G\\2.000000')
And then either:
if line =~ pat
line.gsub!(pat,sub)
puts line
end
or just
puts line if line.gsub!(pat,sub)
since gsub! returns nil if no substitution was performed and the
string if it was.
···
On 6/11/07, Li Chen <chen_li3@yahoo.com> wrote:
1) what is the best scenario to use Regexp.escape?
2) I modify my codes and I get the expected results. But the odd is that
I have to use 3 backslashes before the 2nd 2. How to explain this.
Thanks,
Li
DATA.each_line do |line|
if line=~/1\.000000\\\$P1G\\1\.000000/
line.gsub!(/1\.000000\\\$P1G\\1\.000000/,
'2.000000\\\$P1G\\\2.000000')
puts line
end
end
You use Regexp.escape when you're building up a regular expression
programmatically as a String, and using Regexp.new to turn it into a
regular expression. In that case, all of the control codes in the string
will be interpreted, so if you have a string that needs to be matched
literally, then you use Regexp.escape to escape it first. This is also
the case when using variable interpolation.
On Tue, 12 Jun 2007 02:49:10 +0900, Li Chen wrote:
1) what is the best scenario to use Regexp.escape?
--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/