Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: conditional match in regex

by petral (Curate)
on Nov 05, 2002 at 01:23 UTC ( #210365=note: print w/replies, xml ) Need Help??


in reply to Re: conditional match in regex
in thread conditional match in regex

This also seems to work (at least in perl 5.6.1):
/ ^ ( [\$%@*] | (<) ) ( .* ) (? (2) > ) # ( (2) stands for $2 (the (<) above)) $ /x;


  p

Replies are listed 'Best First'.
Re: Re: Re: conditional match in regex
by jryan (Vicar) on Nov 05, 2002 at 21:47 UTC
    Yes, that will work, but yours has the problem that it creates $1, $2, and $3. I wanted to limit the regular expression so that $1 = type, and $2 = name.
      Right, this way $1 = type and $3 = name.  Oh well...

        p

        Actually, $1 = type if the type is $, @, %, or *; however, in the <name> case, the only way to check its type is by nested conditionals outside the regex; something like:

        if ($2) { if ($1 eq '$') { ... } elsif ($1 eq '@') { ... } ... } else { ... }

        Which, IMHO, is a huge pain in the behind compared to a single if-elsif chain. Why do more work than you have to?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://210365]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-01-20 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?