[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] reg exp fun



Scott Prince wrote:
> 
> I'm playing with a sub routine to parse a script and gather a list of 
> variables, but there's one I can't seem to get a handle on.
> 
> # $q, is the string, in this case...
> #
> # "#$hashref->{'hjkjg'} = "isahashreftoo";3sdfgdkjldskjd sdjkdfsklj"
> 
> while ($q =~ /.*?\$(\w+)-.*?/)
                ^^^        ^^^

.*? at the beginning of a regex is generally unnecessary.  The regex engine
already handles stepping through the target string until it finds a match.

.*? at the end of a regex will never, ever match anything.




> {
> $xref = $1;
> $q =~ s/\$$xref//;
>   if ((!(defined($hashes{$xref}))) && ($xref !~ /arrays|hashes|scalars/))
>   {
>   $hashes{$xref} = 1;
>   }
> }
> 
> I just want to match '$hashref-' or '$hashref->'. I'm pretty sure that 
> the problem is the '-'. Any ideas?

A straightforward regex:

/\$(\w+)->?/

Match one dollar sign.
One or more word characters.
One hyphen.
Zero or one right angle bracket.



You might be interested in the book Mastering Regular Expressions, by
Jeffrey Friedl, published by O'Reilly.  It is an excellent resource for
learning about regular expressions in general, and Perl's in particular.


Ronald

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org