replacing repetition of characters in string

D

David Rio

Hi there,

A friend of mine asked me to write a script that reads a file and
marks the double characters.
Something like:

$ ruby scratch_ruby_drio.rb
INPUT
---------
test. AA. I greatXXrr love it.
audi tt uu yes.
stop here, axmmen.

OUTPUT
------------
{3} test. *AA*. I great*XX**rr* love it.
{2} audi *tt* *uu* yes.
{1} stop here, ax*mm*en.

I coded something up that works but I don't like it:

http://gist.github.com/133299

Do you guys have any other alternatives?

-drd
 
D

David A. Black

Hi --

Hi there,

A friend of mine asked me to write a script that reads a file and
marks the double characters.
Something like:

$ ruby scratch_ruby_drio.rb
INPUT
---------
test. AA. I greatXXrr love it.
audi tt uu yes.
stop here, axmmen.

OUTPUT
------------
{3} test. *AA*. I great*XX**rr* love it.
{2} audi *tt* *uu* yes.
{1} stop here, ax*mm*en.

I coded something up that works but I don't like it:

http://gist.github.com/133299

Do you guys have any other alternatives?

Try this:

str.gsub(/((\w)\2)/,'*\1*')

(Change \w to whatever represents the characters you want to capture.)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
"Ruby 1.9: What You Need To Know" Envycasts with David A. Black
http://www.envycasts.com
 
S

Sebastian Hungerecker

Am Sonntag 21 Juni 2009 00:05:22 schrieb David Rio:
INPUT
---------
test. AA. I greatXXrr love it.
audi tt uu yes.
stop here, axmmen.

OUTPUT

I'm assuming line 2 should be {2} audi *tt* *uu* yes

DATA.each do |line|
i = 0
# For each match of "any character followed by the same character" do:
marked = line.gsub(/(.)\1/) do |match|
i += 1
# return the match surrounded by asterisks from the block, which will
# tell gsub to replace the match with that
"*#{ match }*"
end
puts "{#{ i }} #{marked}"
end
__END__
test. AA. I greatXXrr love it.
audi tt uu yes.
stop here, axmmen.

Output:
{3} test. *AA*. I great*XX**rr* love it.
{2} audi *tt* *uu* yes.
{1} stop here, ax*mm*en.


HTH,
Sebastian
 
D

David Rio

Am Sonntag 21 Juni 2009 00:05:22 schrieb David Rio:

I'm assuming line 2 should be =A0{2} audi *tt* *uu* yes

Yes, that's right. I don't know why it got removed from my initial post.
DATA.each do |line|
=A0i =3D 0
=A0# For each match of "any character followed by the same character" do:
=A0marked =3D line.gsub(/(.)\1/) do |match|
=A0 =A0i +=3D 1
=A0 =A0# return the match surrounded by asterisks from the block, which w= ill
=A0 =A0# tell gsub to replace the match with that
=A0 =A0"*#{ match }*"
=A0end
=A0puts "{#{ i }} #{marked}"
end
__END__
test. AA. I greatXXrr love it.
audi tt uu yes.
stop here, axmmen.

Output:
{3} test. *AA*. I great*XX**rr* love it.
{2} audi *tt* *uu* yes.
{1} stop here, ax*mm*en.

Ah.. much better. Thanks sebastian.

-drd
 
M

Morris Keesan

This problem is a little under-specified, and it would be worth asking
exactly what your friend is trying to accomplish.
As a quick question, what should happen if the input contains THREE of the
same character? Should "aaa" become
aaa
*aa*a
*aaa*
*a*a*a*
or something else?
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top