Gal Hammer wrote:

>On Thu, 19 Sep 2002, Arie Folger wrote:
>
>  
>
>>Ah, I guess you don't realize what I am trying to do. There is an experimental
>>feature in Perl that allows code to be executed within a regex, so that
>>conditional statements can be built in. Such code is announced by (?{ and
>>terminated by }) however the feature is pooorly documented.
>>    
>>
>
>Perl already have such a feature, as I recall. The regex should terminate
>with "e" (eg. s/abc/cba/e). I guess you need to add this control char or
>something else to notify that you're not using a simple regex.
>
>  
>
Not exactly. the 'e' expansion is used to execute code in the 
"replacement" part of  a substituion statemens (s///). it actually turns 
that part into a code statement instead of a single quoted string. What 
Arie is trying to do, is to run perl code in the look up part. it useful 
for many things, but not in the example he's shown us - as the code can 
just be easily executed outside of the regex for more clarity and with 
no difference in the result. zero-width code execution assertion are 
useful for other things, for example
- counting mathces: m|\w(\s(?{$space++}))\w|
- assigning named values for matches: m|href="([^"]+)(?{$url = $^N})"|
- embeding logic in regular expressions (this is a tricky one ;-) :
    s/value="([^"]+)(?{$map{$^N}})".*?name="([^"]+)(?{$valid = ($^R eq 
$^N)})"/$1 . " is " . ($valid?"":"non") . "valid"/e
and probably other uses I couldn't come up with good examples for.

That concludes our 'regular expressions 101' lesson for today, class 
dismissed :-)

-- 
Oded 

::..
buzzword, n:
        The fly in the ointment of computer literacy.



=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to