Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Regexp of Regexp

by mtve (Deacon)
on Jun 02, 2004 at 17:51 UTC ( #359606=sourcecode: print w/ replies, xml ) Need Help??
Category: Fun Stuff
Author/Contact Info
Description:

Regexp that matches regexes. The only difficulty is with /x modifier, so comments in following code intentionally made regexp-valid.

my ($code, $simple, $quanti, $regexp);

$code = qr/(?: (?:
    [^{}]                  # not a curly brackets
|   \{ (??{ $code }) \}    # balanced curly brackets
)* )/x;

$simple = qr/(?:
    \\ [pP] \{ \w+ \}                                       # \p{Prop}
|   \\.                                                     # escape
|   \[ (?: \\. | [^\]] )+ \]                                # [range]
|   \( \? \# [^)]+ \)                                       # (?#text)
|   \( \? [imsx]* -? [imsx]* (?: : (??{ $regexp }) )? \)    # (?imsx-i
+msx)
|   \( \? (?: [:=!>] | <[=!] ) (??{ $regexp }) \)           # (?=patte
+rn)
|   \( \? \?? \{ (??{ $code }) \} \)                        # (?{ code
+ })
|   \( \? \( (?: \d+ | \? [=!<] (??{ $regexp }) | \? \{ (??{ $code }) 
+\} ) \)
        (??{ $regexp }) (?: \| (??{ $regexp }) )? \)        # (?(1)y|n
+)
|   \( (??{ $regexp }) \)                                   # parenthe
+sis
|   [^\\|()\[\]*+?{}]                                       # single c
+har
)/x;

$quanti = qr/(?:
    (?: $simple (?: [+*?] \?? | \{ \d+ (?: , \d* )? \} \?? )?
)* )/x;

$regexp = qr/(?:
    $quanti (?: \| $quanti )*
)/x;

print $regexp =~ /^$regexp$/;
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2016-08-27 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (380 votes). Check out past polls.