Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Think about Loose Coupling
 
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 making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2014-04-23 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (541 votes), past polls