Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Useful Uses of regex code expressions?

by jryan (Vicar)
on Jun 26, 2002 at 23:22 UTC ( #177585=note: print w/ replies, xml ) Need Help??


in reply to Useful Uses of regex code expressions?

The reason (?{}) and (??{}) aren't used to often is that they are extremely powerful, and most data parsing simply doesn't need that kind of power (or, when it does, most people will turn to Parse::RecDescent). However, I've found the code expression abilty to be quite helpful in employing a few slick tricks, such as:

  1. Conditional Insert: (??{(cond) ? yes : no})

    I'm not talking about (?(cond) yes|no), of course; (?(cond) yes|no) will only take a backreference or a zero width assertion as its condition. (??{(cond) ? yes : no}) will similary cause the appropriate pattern to be inserted based on the evaluation of the condition; however, the condition is no longer limited. This is useful in such cases where you don't want to fail if are using the (?(cond)yes) of (?cond), or are mucking with much weirder other things... (see my homenode for an example)

  2. Partial Backreferences: (??{substr($string_that_is_matched_against,$+[$#+]-1,1)})

    The snippet above will insert the last character was matched when the regex hits that point. Above is a general case, useful if you are playing with recursion; if you know $DIGIT, you can always do:

    (??{substr($DIGIT,-1)}

  3. Dynamically Building Character Classes: (??{'[\Q$letters\E]*'})

    Variables normally can't be interpolated within character classes. With (??{}), now you can.

  4. Matching/extracting arbitrarily nested elements with recursion: (??{$compiled_regex})

    Like the anonymous monk already said, (??{}) is a necessity in matching nested sets of paren/brackets/etc if you don't want to go crawling through the string C-style. I go into more detail using a less basic example than the one in perlre here.

I have other "interesting" regex on my homenode, and many involve our friends (??{}) and (?{}). Take a look if you don't feel like sleeping well tonight.


Comment on Re: Useful Uses of regex code expressions?
Select or Download Code
Re: Re: Useful Uses of regex code expressions?
by I0 (Priest) on Jun 26, 2002 at 23:37 UTC
    (??{}) is handy in matching nested sets of paren/brackets/etc without crawling through the string C-style, but not a necessity.
      True; but rolling with your flow, regexes themselves aren't really a necessity - only something that happens to be handy for when when you don't feel like crawling through a string C-style.
        Regexes without (??{}) can be used in matching nested sets of paren/brackets/etc when you don't feel like crawling through a string C-style.
Re: Re: Useful Uses of regex code expressions?
by kvale (Monsignor) on Jun 27, 2002 at 03:28 UTC
    Very nice! I especially like the partial backreferences.

    It is possible to dynamically build character classes the ordinary way:
    foreach my $char (a..d) { print "bbb matches $char\n" if "bbb" =~ /[$char]/; }
    prints
    bbb matches b

    Thanks for expanding my horizons!

    -Mark
Re: Re: Useful Uses of regex code expressions?
by japhy (Canon) on Jun 27, 2002 at 14:29 UTC
    A few corrections:

    1. The (?(...)true|false) can also take a (?{ code }) block as its condition.

    3. Uh, /[\Q$foo\E]/ works for me. The problem comes when you want to employ variables that are created as the regex matches (like the digit variables). Then it comes in handy. For example, /.(??{ "[^\Q$&\E]" })*/s finds a string of characters with no repeats.

    You can see examples of code assertions in my book, whenever the hell I finish it.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Regarding 1: That most certainly is an undocumented feature. According go perlre:

      (?(condition)yes-pattern|no-pattern) (?(condition)yes-pattern) Conditional expression. (condition) should be either an integer in par +entheses (which is valid if the corresponding pair of parentheses mat +ched), or look-ahead/look-behind/evaluate zero-width assertion.

      Update: I saw, and read that phrase; however, my brain hates me and skipped processing it correctly ;(

        Regarding 1: That most certainly is an undocumented feature

        It is documented in the chunk of perlre you quoted

        ... or look-ahead/look-behind/evaluate zero-width assertion.

        It can be an integer or

        • a look-ahead zero width assertion
        • a look-behind zero width assertion
        • an evaluate zero width assertion

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-12-22 12:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (116 votes), past polls