Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Regexp of Regexp

by mtve (Chaplain)
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$/;

Comment on Regexp of Regexp
Download Code

Back to Code Catacombs

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 having an uproarious good time at the Monastery: (7)
As of 2015-07-08 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls