开发者

Why doesn't this Ruby replace regex work as expected?

Consider the following string which is a C fragment in a file:

strcat(errbuf,errbuftemp);

I want to replace errbuf (but not errbuftemp) with the prefix G-> plus errbuf. To do that successfully, I check the character after and the character before errbuf to see if it's in a list of approved characters and then I perform the replace.

I created the following Ruby file:

line = " strcat(errbuf,errbuftemp);"  
item = "errbuf"  
puts line.gsub(/([ \t\n\r(),\[\]]{1})#{item}([ \t\n\r(),\[\]]{1})/, "#{$1}G\->#{item}#{$2}")

Expected result:

strcat(G->errbuf,errbuftemp);

Actual result

strcatG->errbuferrbuftemp);

Basically, the matched characters before and after errbuf are not reinserted back with the replace expression.

Anyone can point out what I'm doing wrong?开发者_开发技巧


Because you must use syntax gsub(/.../){"...#{$1}...#{$2}..."} or gsub(/.../,'...\1...\2...').

Here was the same problem: werid, same expression yield different value when excuting two times in irb

The problem is that the variable $1 is interpolated into the argument string before gsub is run, meaning that the previous value of $1 is what the symbol gets replaced with. You can replace the second argument with '\1 ?' to get the intended effect. (Chuck)


I think part of the problem is the use of gsub() instead of sub().

Here's two alternates:

str = 'strcat(errbuf,errbuftemp);'

str.sub(/\w+,/) { |s| 'G->' + s } # => "strcat(G->errbuf,errbuftemp);"
str.sub(/\((\w+)\b/, '(G->\1') # => "strcat(G->errbuf,errbuftemp);"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜