regex utility

A

Alythh

I am frankly tired to debug my regexes by running the whole program
all the times.
I found some interesting sites where I can form-input a string of text
and a regex - and see if it matches.

Do you know of any utility to be run locally, possibly able evn to
report - it match is OK - the $1, $2, ... values?


Thanks!

Alessandro Magni
 
R

Richard Gration

I am frankly tired to debug my regexes by running the whole program all
the times.
I found some interesting sites where I can form-input a string of text
and a regex - and see if it matches.
Do you know of any utility to be run locally, possibly able evn to
report - it match is OK - the $1, $2, ... values? Thanks!
Alessandro Magni

Well, I thought for a bit and wrote this ...


#!/usr/bin/perl

use strict;
use warnings;

unless ($#ARGV == 1) {
usage();
exit;
}

my $matched = $ARGV[1] =~ m($ARGV[0]);

if ($matched) {
print "Your text matches the pattern\n";
defined $1 and print "match 1: $1\n";
defined $2 and print "match 1: $2\n";
defined $3 and print "match 1: $3\n";
defined $4 and print "match 1: $4\n";
defined $5 and print "match 1: $5\n";
defined $6 and print "match 1: $6\n";
defined $7 and print "match 1: $7\n";
defined $8 and print "match 1: $8\n";
defined $9 and print "match 1: $9\n";
}

sub usage {
print <<EOUSAGE
Usage:

$0 <pattern> <text>
EOUSAGE
}
 
M

Mark Clements

Alythh said:
I am frankly tired to debug my regexes by running the whole program
all the times.
I found some interesting sites where I can form-input a string of text
and a regex - and see if it matches.

Do you know of any utility to be run locally, possibly able evn to
report - it match is OK - the $1, $2, ... values?
have you tried the command line?

eg

perl -nle 'print $1 if /(\w+)/'

perldoc perlrun
 
R

Richard Gration

Spot the deliberate mistake ...

defined $2 and print "match 1: $2\n";
^

should be 2 of course, etc.
 
T

Thomas Kratz

Richard said:
I am frankly tired to debug my regexes by running the whole program all
the times.
I found some interesting sites where I can form-input a string of text
and a regex - and see if it matches.
Do you know of any utility to be run locally, possibly able evn to
report - it match is OK - the $1, $2, ... values? Thanks!
Alessandro Magni


Well, I thought for a bit and wrote this ...


#!/usr/bin/perl

use strict;
use warnings;

unless ($#ARGV == 1) {
usage();
exit;
}

my $matched = $ARGV[1] =~ m($ARGV[0]);

if ($matched) {
print "Your text matches the pattern\n";
defined $1 and print "match 1: $1\n";
defined $2 and print "match 1: $2\n";
defined $3 and print "match 1: $3\n";
defined $4 and print "match 1: $4\n";
defined $5 and print "match 1: $5\n";
defined $6 and print "match 1: $6\n";
defined $7 and print "match 1: $7\n";
defined $8 and print "match 1: $8\n";
defined $9 and print "match 1: $9\n";
}

or simpler:

my @matches = $ARGV[1] =~ m($ARGV[0]);
my $i = 1;
print "match $i++: $_\n" for map {
defined($_) ? $_ : 'undef'
} @matches;
print "no matches\n" unless @matches;

Thomas
 
T

Thomas Kratz

Thomas said:
or simpler:

my @matches = $ARGV[1] =~ m($ARGV[0]);
my $i = 1;
print "match $i++: $_\n" for map {

oops, make that:
print "match ", $i++, ": $_\n" for map {
defined($_) ? $_ : 'undef'
} @matches;
print "no matches\n" unless @matches;

Thomas
 
M

Michele Dondi

I am frankly tired to debug my regexes by running the whole program all
the times.
I found some interesting sites where I can form-input a string of text
and a regex - and see if it matches.
Do you know of any utility to be run locally, possibly able evn to
report - it match is OK - the $1, $2, ... values? Thanks!
Alessandro Magni

Well, I thought for a bit and wrote this ... [snip]
my $matched = $ARGV[1] =~ m($ARGV[0]);

Also, see 'perldoc -q pass/return' under the heading "Passing
Regexes", especially the bit about using an exception-trapping eval().


Michele
 
M

Michele Dondi

#!/usr/bin/perl

use strict;
use warnings;

unless ($#ARGV == 1) {

Huh?!? Not that this is technically incorrect, but is there any good
reason for not using '@ARGV == 2' instead? (Which IMHO conveys more
naturally the idea that the number of required parameters is two.)
usage();
exit;
} [...]
sub usage {
print <<EOUSAGE
Usage:

$0 <pattern> <text>
EOUSAGE
}

Also, I understand that you're being more verbose than would seem
natural for this simple because you're probably used to write "bigger"
applications, but since it would be better, still IMHO, to die()
anyway, then I'd write:

die <<"EOUSAGE" unless @ARGV == 2;
Usage:

$0 <pattern> <text>
EOUSAGE

Also, you can still adopt a "hybrid" approach writing a usage() sub
to *return* the usage string or "save" that to a $usage scalar in case
you need it in multiple places of your script, e.g.:

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Std;

sub usage(); # Or (maybe better!) 'use constant'...

my %opts;
getopts 'h', \%opts;
print usage and exit if $opts{h};

die usage unless @ARGV == 2;

# ACTUAL PROGRAM...

sub usage () {
<<"EOUSAGE"
Usage: $0 [options] <pattern> <text>

Currently the only option is -h, that
will print this help and exit immediately.
EOUSAGE
}
__END__


BTW: I didn't use it here, but Getopt::Std has its own --help managing
facility, that often come handy and can complement an approach like
that above...


Michele
 
R

Richard Gration

<SNIP valid stylistic critique>

The reason that code snippet looks like it did is 'cos I wrote it in 2
minutes as a diversion from the actual coding I'm doing at the moment
which, surprise, surprise, contains several $#$array_ref sort of things
so it just carried over.

I happen to think that not using an array to hold the return from the
match expression was the most serious error. That did try to break
through my mind's limina while I was doing the copy and paste, but I was
in a hurry ...

R
 
C

Chris Mattern

Christian said:
Thomas said:
or simpler:

my @matches = $ARGV[1] =~ m($ARGV[0]);
my $i = 1;
print "match $i++: $_\n" for map {
defined($_) ? $_ : 'undef'
} @matches;
print "no matches\n" unless @matches;

or even:

my $c;
print "match ",++$c,": $_$/" for($ARGV[1]=~m($ARGV[0])g);
Such a lovely day for golf, isn't it?
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top