Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4612

Ruby: replacement part include $1 $2 $3 and saved in a variable, but interpolation doesn't happen when using gsub

$
0
0

(a) the codes I wrote.

$str = '909090 aa bb cc dd ee ff 00 12345678 aa bb 12345678 aa bb cc dd ee ff 00 11 22 33 123456 FE 89'puts $str$str.gsub!(/\s+/, '')search_1  = 'aa bb cc dd ee ff 00 (\S{8} aa bb \S{8} aa bb cc dd ee ff 00 11 22 33 \S{6}) (FF|FE) (89)'puts search_1search_1.gsub!(/\s+/, '')replace_1 = '11 22 33 44 55 66 77 $1 $2 $3'puts replace_1replace_1.gsub!(/\s+/, '')repls_1 = [  [search_1, replace_1],]repls_1.each do |x|  abc = $str.gsub(Regexp.new(x[0])) {    x[1]  }  puts abcend

(2) script running results

  909090 aa bb cc dd ee ff 00 12345678 aa bb 12345678 aa bb cc dd ee ff 00 11 22 33 123456 FE 89  aa bb cc dd ee ff 00 (\S{8} aa bb \S{8} aa bb cc dd ee ff 00 11 22 33 \S{6}) (FF|FE) (89)  11 22 33 44 55 66 77 $1 $2 $3  90909011223344556677$1$2$3

The interpolation for $1, $2, $3 doesn't happen, is there way to solve it? Thanks.

(i) I don't want use \1 \2 \3, but $1 $2 $3)

(ii) There're more than one patterns, here only one example

(iii) matched parts numbers is not fixed, here is $1$2$3, other case is possible, such as ...$1...$2...$3...$5..., hope the codes can cover this variation on both captured parts number and position.


Viewing all articles
Browse latest Browse all 4612

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>