2002-01-09 Update: Note to Posterity - there are better ways than this.
After posting this little thing (which truly was helpful for some),
two finefolks were kind enough to point out a
better way of understanding your regex and
a fine online reference - neither of which I had managed to locate on my own.
Many thanks to japhy for creating them and to tilly and crazyinsomniac for pointing them out.
If you're here looking to learn more about regular expressions, you'll do well to follow those links.
A new guy here was confused by the difference between
"capturing" with parens and "grouping" with brackets. I gave him this function to help show the difference, but it's useful for general testing of regular expressions.
The thing to remember is that it's easier to write a regex
that matches what you're looking for than it is to write
one that also doesn't match what you're not looking for.
Note: I did not use the 'quote regex' qr//; because that makes print display the regex in a way that differs from what the user typed. My goal here is clarity.
Further Reading (in increasing order of difficulty):
#!/usr/bin/perl -w
use strict;
sub matches_regex{
## Purpose: Means to test wether a Regular Expression
## matches what you think it matches.
## Requires: One regex and one a scalar to use it on.
## Returns: True (1) for matches, false (0) for non-matches.
## ==========================================================
## Usage: "matches_regex( 'string to test', 'regex to use' );
## ==========================================================
my ($string, $regex) = @_;
return( $string =~ /$regex/ );
}
# ===== illustrations =====
my $string = '4245581234567890';
my @regexes = ('^[424558][0-9]{10,10}$'
,'^(424558)[0-9]{10,10}$'
,'^424558[0-9]{10,10}$'
);
foreach my $regex( @regexes ){
print "Testing string '$string' for regex '$regex'\n";
if( matches_regex( $string, $regex ) ){
print "\tMatched\n";
} else {
print "\tDid not match\n";
}
}
For people learning regular expressions the resource you really want them to pick up is japhy's YAPE::Regex::Explain which will take a regular expression, break it up, and insert comments telling you how perl will understand what you wrote.
I tried it, tilly. That is great!. It's very easy to use, and I love the plain english explanations. Much more helpful as a teaching aid than what I posted here.
Thanks for the pointer, and thank you, japhy, for the modules!
And what exactly is so useful? Why a sub, when it does just a little? Wouldn't $string =~ /$regex/ be a LOT clearer and easier?
By the way, parens can be used for either grouping (foo|bar) and capturing. How did you explain the difference between the two without using both?
If demonstrating regexes, it might be good to use $&.