scan regular expression and strings

S

STEPHEN BECKER I V

tr="meet"
tr.scan(/(.)\1/) {|x| x=x+'x'}
print tr

What I want it to do is find doubles like the ee in meet and put an x
between them. Am i doing this wrong because x is part of an array now?
 
B

Brian Schröder

STEPHEN said:
tr="meet"
tr.scan(/(.)\1/) {|x| x=x+'x'}
print tr

What I want it to do is find doubles like the ee in meet and put an x
between them. Am i doing this wrong because x is part of an array now?
Your change to x is local to the block, because x is a new string object
on each iteration, that is not connected to tr.
You can achieve your aim like this:

Warning: Untested, but the idea should be clear.

x = "meet meep meep"
y = x.gsub(/(.)(\1)/, '\1x\2')

Observe that this does not change the string x, but the result will be
allocated in y.
If you want to change the original string use gsub!.

Regards,

Brian
 
A

Andrew Johnson

tr="meet"
tr.scan(/(.)\1/) {|x| x=x+'x'}
print tr

What I want it to do is find doubles like the ee in meet and put an x
between them. Am i doing this wrong because x is part of an array now?

You want to use .gsub! instead of .scan. A simple approach:

tr="meet"
tr.gsub!(/(.)(\1)/, '\1X\2')
print tr

Of course if you want "meeet" to become "meXeXet" then the above
won't be good enough and you'll want to use look-aheads to avoid
consuming each following 'e':

tr="meeet"
tr.gsub!(/(.)(?=\1)/, '\1X')
print tr

regards,
andrew
 
R

Ryan Flynn

x = "meet meep meep"
y = x.gsub(/(.)(\1)/, '\1x\2')

$ irb
irb(main):001:0> $_="meeet"
=> "meeet"
irb(main):002:0> gsub(/(.)(\1)/, '\1x\2')
=> "mexeet"
irb(main):003:0> gsub(/(.)\1/, '\1x\1')
=> "mexexet"
irb(main):004:0>

i figured it'd be more efficient not to capture the second instance,
and just use \1 again in the replacement since it's the same
character; however, i also got a different ("more correct" i think)
result... do ruby regexes not advance past what they don't capture? i
expected the second one to be slightly more efficient but i expected
the behavior to be identical... can anyone shed some light?
 
T

ts

R> $ irb
R> irb(main):001:0> $_="meeet"
R> => "meeet"
R> irb(main):002:0> gsub(/(.)(\1)/, '\1x\2')
R> => "mexeet"
R> irb(main):003:0> gsub(/(.)\1/, '\1x\1')
R> => "mexexet"
R> irb(main):004:0>

svg% irb
irb(main):001:0> $_="meeet"
=> "meeet"
irb(main):002:0> gsub(/(.)\1/, '\1x\1')
=> "mexeet"
irb(main):003:0> gsub(/(.)(\1)/, '\1x\2')
=> "mexexet"
irb(main):004:0>

R> the behavior to be identical... can anyone shed some light?

You work with $_ which is modified, i.e.

svg% irb
irb(main):001:0> $_="meeet"
=> "meeet"
irb(main):002:0> gsub(/(.)\1/, '\1x\1')
=> "mexeet"
irb(main):003:0> $_
=> "mexeet"
irb(main):004:0> gsub(/(.)\1/, '\1x\1')
=> "mexexet"
irb(main):005:0> $_
=> "mexexet"
irb(main):006:0>




Guy Decoux
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top