Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 rifling through the Monastery: (10)
As of 2015-03-31 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:









    Results (660 votes), past polls