On 24/08/2011 15:54, Ramprasad Prasad wrote:

Assume I have to find the first unique character in a string

$string = "abtable";

# t is the first unique string

I tried using a negative backtrace lookup to get the answer in a
single regex ... But something is missing.

/(.).(?!\1)/&&  print $1;

it seems fine ... But doesn't work

Your regex matches any two characters that are not followed by the first
of those two characters. So it finds 'ab' and sees that 't' follows,
which is not the same as 'a' so a match has been found.

You mean to match any single character that is not followed by a string
containing itself. Like this

use strict;
use warnings;

my $string = "abtable";

print $1 if $string =~ /(.)(?!.*\1)/;

__END__

Output is 't'.

HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to