Bart said:
Hello,
I got intrigued by the following challenge which seems to be
impossible.
Is it possible to write a regular expression that replaces all
occurences of a character except the first occurence ?
a''bc'def'g' -> a'bcdefg
'''ab'cd'efg -> 'abcdefg
abc'd'e''f'g -> abc'defg
etc.
I assume you mean that it'll have to locate any character that's
repeated (not a predetermined one) and only remove those additional
occurrences, and only use a regular expression?
I'd use a different method to accomplish the task, but it is indeed
interesting to try and do this only with a regex.
The following should work and only remove the second or higher instance
of any character (so you a string with "'''sfsf'et'st464y'''" will
result in "'sfet46y". Pretty cool, eh?
Here's the regex logic:
$string =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;
Here's an example based on your strings, using only a regular
expression:
script.pl:
!/usr/bin/perl
use warnings;
use strict;
my $linea = "a''bc'def'g'";
my $lineb = "'''ab'cd'efg";
my $linec = "abc'd'e''f'g";
print "$linea -> ";
$linea =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;
print "$linea\n";
print "$lineb -> ";
$lineb =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;
print "$lineb\n";
print "$linec -> ";
$linec =~ s|(.)| ($` =~ m/$1/) ? '' : $1 |eg;
print "$linec\n";
The output:
~]$ ./script.pl
a''bc'def'g' -> a'bcdefg
'''ab'cd'efg -> 'abcdefg
abc'd'e''f'g -> abc'defg
Is that what you were trying to do?