abhishes has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I have a string like

"The The duplicate duplicate string string .."

I want to convert this to

"The duplicate string ."

I wrote the following perl program to do this but it does not remove the duplicate "." character in the end of the string
use warnings; use strict; $_ = 'The The duplicate duplicate string string ..'; s/\b((\w)+ | (.)+) \1\b/$1/g; print "$_\n";
regards,
Abhishek.

Replies are listed 'Best First'.
Re: duplicate string with regular expression
by BrowserUk (Patriarch) on Dec 03, 2002 at 05:14 UTC

    There are a couple of problems with your regex.

    • One is that you need to escape the dot '\.' in order for it to only match a literal '.' as it is a regex metachar.
    • Another is that your duplicate words are seperated by a space, which you wish to capture and discard, but the duplicate fullstops have no space between them.

    This works for your sample data.

    s/((?:\w+ )|\.)\1/$1/g;

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: duplicate string with regular expression
by BUU (Prior) on Dec 03, 2002 at 05:08 UTC
Re: duplicate string with regular expression
by pg (Canon) on Dec 03, 2002 at 05:42 UTC
    Just a different flavor of the same thing:
    s/((?:(?:\w+\s+){2})|\.{2})/substr($1, 0, length($1)\/2)/eg;