Replacing repetition of characters in string

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 greatXX**rr love it.
{2} audi tt uu yes.
{1} stop here, axmmen.

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

Do you guys have any other alternatives?

-drd

Hi –

On Sun, 21 Jun 2009, David Rio wrote:

audi tt uu yes.
133299’s gists · GitHub

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

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

{1} stop here, axmmen.
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 greatXX**rr love it.
{2} audi tt uu yes.
{1} stop here, axmmen.

HTH,
Sebastian

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
aaa
aaa
aaa
or something else?

On Sat, Jun 20, 2009 at 5:44 PM, Sebastian
Hungerecker[email protected] wrote:

{2} audi tt uu yes.
{1} stop here, axmmen.

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

Yes, that’s right. I don’t know why it got removed from my initial post.

end
END
test. AA. I greatXXrr love it.
audi tt uu yes.
stop here, axmmen.

Output:
{3} test. AA. I greatXX**rr love it.
{2} audi tt uu yes.
{1} stop here, axmmen.

Ah… much better. Thanks sebastian.

-drd

On Sat, Jun 20, 2009 at 10:18 PM, Morris Keesan[email protected]
wrote:

This problem is a little under-specified,

Yes, my bad.

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
aaa
aaa
aaa
or something else?

aaa, which is what Sebastian’s code does.

-drd

Hi –

On Sun, 21 Jun 2009, David Rio wrote:

aaa
aaa
aaa
or something else?

aaa, which is what Sebastian’s code does.

Mine does too (I lucked out :slight_smile:

str = “Helllo”
str.gsub(/((\w)\2)/,’\1’) # “Helllo”

David