Monks ~
I wanted to write a test to see if a given scalar contains a quoted regular expression. I don't care if the regex is valid or what it matches as long as perl thinks it's a real quoted regex. I discovered that it's either much easier than I thought, or I'm missing something. Or both :)
#!/usr/bin/perl use strict; use warnings; my %qrs = ( string1 => 'foo', string2 => '(xism)', regex => qr/foo/, 'regex?' => '(?-xism:foo)', ); while( my( $key, $regex ) = each %qrs ) { print "'$key' "; print $regex =~ /\(\?\-xism:/ ? 'YES: ' : 'no: '; print $regex; print ' (and matches)' if 'foo' =~/$regex/; print "\n"; }
And here's the output:
'regex?' YES: (?-xism:foo) (and matches) 'regex' YES: (?-xism:foo) (and matches) 'string1' no: foo (and matches) 'string2' no: (xism)
It looks like creating a scalar of the pattern "(?-xism:$STUFF)" is enough to make perl believe it's a quoted regex. I've tried to find a definitive in perldoc, man pages, The Camel, etc. answer and haven't.
Is it really this cool and simple?
In reply to Walks like a regex, quacks like a regex... by legLess
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |