[tpm] Solved: Re: regex problem

Madison Kelly linux at alteeve.com
Fri Jul 3 11:53:27 PDT 2009


Why is always that, once I give up and ask for help, I find the answer 
just minutes later? I was banging my head for an hour before posting 
this question...

Of course, '(.*?)' matches anything, 0 or more times. Take the '?' out 
or adding and end of string anchor fixed it.

Sorry for the line noise.

Madi

Madison Kelly wrote:
>   Could someone riddle me why this isn't working?
> 
> #----------------------
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my $search_word="*4766";
> my $joined_string="905-555-4766#:#jane die#:##:#1#:#18#:#2009-06-14 
> 13:44:33.617008#:#";
> my $match=0;
> 
> if ($search_word=~/^\*(.*?)\*$/)
> {
>     # Match anywhere.
>     my $local_search_word=$1;
>     print __LINE__.": local_search_word: [$local_search_word]\n";
>     $match++ if $joined_string =~ /$local_search_word/;
> }
> elsif ($search_word=~/^\*(.*?)/)
> {
>     # Match the end of the strings.
>     my $local_search_word=$1;
>     print __LINE__.": local_search_word: [$local_search_word]\n";
>     $match++ if $joined_string =~ /$local_search_word#:#/;
> }
> elsif ($search_word=~/(.*?)\*$/)
> {
>     # Match the start of the strings.
>     my $local_search_word=$1;
>     print __LINE__.": local_search_word: [$local_search_word]\n";
>     $match++ if $joined_string =~ /#:#$local_search_word/;
> }
> else
> {
>     # Match the strings exactly.
>     $match++ if $joined_string =~ /#:#$search_word#:#/;
> }
> 
> exit;
> #----------------------
> 
>   When I run, I see that the second conditional matches because if '22: 
> ' being printed, but the $1 seems to be empty. According to 'perldoc 
> perlre' I seem to be doing it right... Results say otherwise though. :)
> 
> Thanks!
> 
> Madi



More information about the toronto-pm mailing list