in reply to A Regex to identify Regex's / Compiling a regex

Here is what I use sometimes, which is not perfect as you can't use brackets inside a regexp, I have to add level counting (comments and patches welcome!). You could also have a look at Regexp::Common::Balanced.

#!/bin/perl -w use strict; my %brace=( '[' => ']', '(' =>')', '{' => '}', '<' => '>'); my $not_brace_class= '[^\\' . join( '\\', keys %brace) . ']'; my $SIMPLE_REGEXP = "(($not_brace_class)(?:(\\\\.|.)*?)\\2)"; my @regexp= ($SIMPLE_REGEXP); while( my( $open, $close)= each( %brace)) { push @regexp, "(\\$open(?:\\\\.|.)*?\\$close)"; } while( <DATA>) { chomp; my( $exp, $expected_match)= split /\t+/; print "$exp: "; foreach my $regexp (@regexp) { if( $exp=~ m{^$regexp}) { my $found= $1; if( $found eq $expected_match) { print "REGEXP OK "; } else { print "REGEXP matches $found instead of $expected_match (r +egexp is $regexp)"; } last; } } print "\n"; } __DATA__ /toto/ /toto/ #toto# #toto# {toto} {toto} /to\to/ /to\to/ /to\/to/ /to\/to/ #to\to# #to\to# #to\#to# #to\#to# [to\to] [to\to] [to\]to] [to\]to] [to[to]] [to[to]] /toto/no /toto/ #toto#no #toto# {toto}no {toto} /to\to/no /to\to/ /to\/to/no /to\/to/ #to\to#no #to\to# #to\#to#no #to\#to# [to\to]no [to\to] [to\]to]no [to\]to] [to[to]]no [to[to]] /toto/no/ /toto/ #toto#no# #toto# {toto}no{no {toto} {toto}no{}no {toto} {toto}no}no {toto} {toto}no{ {toto} {toto}no{} {toto} {toto}no} {toto} /to\to/no/ /to\to/ /to\to/no\/ /to\to/ /to\/to/no/ /to\/to/ #to\to#no\# #to\to# #to\#to#no# #to\#to# [to\to]no[ [to\to] [to\]to]no] [to\]to] [to[to]]no[] [to[to]]