Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Regex Checker

by mrbbking (Hermit)
on Jan 08, 2002 at 18:45 UTC ( #137136=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info /msg mrbbking

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 fine folks 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}$'

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";
Replies are listed 'Best First'.
Re (tilly) 1: Regex Checker
by tilly (Archbishop) on Jan 08, 2002 at 21:47 UTC
    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.

    Try it. Seriously.

      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!

Re: Regex Checker
by Juerd (Abbot) on Jan 08, 2002 at 20:23 UTC
    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 $&.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://137136]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2018-02-18 18:37 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (256 votes). Check out past polls.