regex negative looking up a backtrace
regex negative looking up a backtrace
am 24.08.2011 16:54:00 von Ramprasad Prasad
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
--
Sent from my mobile device
Thanks
Ram
n
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: regex negative looking up a backtrace
am 24.08.2011 17:16:49 von Shlomi Fish
Hi Ram,
On Wed, 24 Aug 2011 20:24:00 +0530
Ramprasad Prasad wrote:
> Assume I have to find the first unique character in a string
>=20
In that case, use a hash. Is there any reason you want to do it using a reg=
ex?
Regards,
Shlomi Fish
=20
> $string =3D "abtable";
>=20
> # t is the first unique string
>=20
> I tried using a negative backtrace lookup to get the answer in a
> single regex ... But something is missing.
>=20
> /(.).(?!\1)/ && print $1;
>=20
> it seems fine ... But doesn't work
>=20
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap
Knuth is not God! Typing â=9CGodâ=9D into Google and pressing =E2=
Iâ=99m Feeling Luckyâ=9D
will not lead you to his homepage.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: regex negative looking up a backtrace
am 24.08.2011 17:23:07 von Chris Charley
>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
>
>--
>Sent from my mobile device
>
>Thanks
>Ram
Hello Ram
You were almost there.
$string =~ /(.)(?!.*\1)/;
Chris
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: regex negative looking up a backtrace
am 24.08.2011 17:23:08 von Rob Dixon
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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: regex negative looking up a backtrace
am 25.08.2011 03:33:49 von Ramprasad Prasad
still does not work
$_ = "zzabtable";
/(.)(?!.*\1)/ && print "$1\n";
Output is z
On 8/24/11, Chris Charley 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
>>
>>--
>>Sent from my mobile device
>>
>>Thanks
>>Ram
>
> Hello Ram
>
> You were almost there.
>
> $string =~ /(.)(?!.*\1)/;
>
> Chris
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
--
Sent from my mobile device
Thanks
Ram
n
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: regex negative looking up a backtrace
am 25.08.2011 12:43:45 von timothy adigun
--0016e6dd8a2e627a0204ab521aff
Content-Type: text/plain; charset=ISO-8859-1
Hi Prasad,
>>still does not work
>>$_ = "zzabtable";
>>/(.)(?!.*\1)/ && print "$1\n";
>>Output is z
The script above worked just has it was intended. The output should be 'z'
and not 't' as you suppose. Your code will match the first unique string
that is not repeated, after other letters. And in the case above 'z' is the
string not 't'.
if you use:
$_ = "zzabtablez";
/(.)(?!.*\1)/ && print "$1\n"; # output 't'
You should have 't' as intended, because 'z' was repeated in the string,
after others.The same reason
>>my $string = "abtable";
>>print $1 if $string =~ /(.)(?!.*\1)/; # print t
worked.
Prasad, I don't know what you are getting at, but I suppose what you want is
the first occurrence of a unique string, that occur just once in the given
string. If my assumption is correct then try the code below and see if it
helps:
use strict;
use warnings;
my $string = 'thequickbrownfoxjumpovertheoldlazydog';
my @arr=split //,$string;
my %hash;
foreach my $occur(@arr){ # use hash to get number of occurrence of each
letter
if(exists $hash{$occur}){
$hash{$occur}++;
}else{$hash{$occur}=1}
}my @arr_new;
while(my($key,$value)=each %hash){
push @arr_new,$key if $value==1; # get letter with only one occurrence
}my @pos;
for my $found(@arr_new){
push @pos,index($string,$found); # get the index position of all the
letter with 1
# occurrence in the
orignial string
}
@pos=sort {$a<=>$b}@pos; # sort your index
print substr($string,$pos[0],1); # print out 'q'
--0016e6dd8a2e627a0204ab521aff--
Re: regex negative looking up a backtrace
am 26.08.2011 20:01:11 von Peter Scott
On Wed, 24 Aug 2011 20:24:00 +0530, Ramprasad Prasad wrote:
> Assume I have to find the first unique character in a string
>=20
> $string =3D "abtable";
>=20
> # t is the first unique string
>=20
> I tried using a negative backtrace lookup to get the answer in a single
> regex ... But something is missing.
>=20
> /(.).(?!\1)/ && print $1;
>=20
> it seems fine ... But doesn't work
The easier way would be with code instead of a regex:
use strict;
use warnings;
use 5.10.0;
@ARGV or @ARGV =3D (qw(abtable zzabtable abcabc zbbb abcdefabcdefg q qqq)=
);
OUTER: for ( @ARGV )
{
my @c =3D split //;
my %n;
$n{$_}++ for @c;
for ( @c )
{
next if $n{$_} > 1;
say;
next OUTER;
}
say "FAIL: $_";
}
% ./proggie
t
t
FAIL: abcabc
z
g
q
FAIL: qqq
But if you really want a regular expression to do the job:
use strict;
use warnings;
use 5.10.0;
@ARGV or @ARGV =3D (qw(abtable zzabtable abcabc zbbb abcdefabcdefg q qqq)=
);
for ( @ARGV )
{
say
/\A # Starts with
(.*?) # Minimal string, possibly null, then
(.) # Candidate character
(??{ # Evaluate code
index($1,$2) >=3D 0 # If first capture contains candidate
? '(*FAIL)' # Assert match failure
: '(*ACCEPT)' # Else success
}) # Followed by
(?!.*\2.*) # Chars not containing candidate
/x ? "$1\[$2\]${^POSTMATCH}" : "FAIL: $_";
}
%proggie
ab[t]able
zzab[t]able
FAIL: abcabc
[z]bbb
abcdefabcdef[g]
[q]
FAIL: qqq
--=20
Peter Scott
http://www.perlmedic.com/ http://www.perldebugged.com/
http://www.informit.com/store/product.aspx?isbn=3D0137001274
http://www.oreillyschool.com/certificates/perl-programming.p hp
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/