PrePAN

Sign in to PrePAN

Regex::Wrapper OO Perl Regex Wrapper

Author
snovakov@github
Date
URL
Status
In Review
Good

Synopsis

INPUT

#!/usr/bin/perl
use strict;
use warnings;
use Wrapper;
use Data::Dumper;

my $original = "cie";
my $regex = Wrapper->new($original)
	or die("Error creating Wrapper object!");
print "'regex' object :\n";
print Dumper($regex);
print Dumper($regex->get_regex());
print "---------------------------\n";
print "regex->match (full):\n";
print Dumper($regex->match("cie")->get_match);
print "regex->match('cie')->from:\n";
print $regex->match("cie")->from()."\n";
print "regex->match('cie')->to:\n";
print $regex->match("cie")->to()."\n";
print "---------------------------\n";
print "regex->match (partial):\n";
print Dumper($regex->match("ci")->get_match);
print "---------------------------\n";
print "regex->match('ci')->from:\n";
print $regex->match("ci")->from()."\n";
print "regex->match('ci')->to:\n";
print $regex->match("ci")->to()."\n";
print "regex->match('ci')->subpatterns(): \n";
print Dumper($regex->match("ci")->subpatterns(0));
print "---------------------------\n";
print "regex->match (no match):\n";
defined ($regex->match("xyz")) ? () : goto SKIP;
print Dumper($regex->match("xyz")->get_match);
print "regex->match('xyz')->from:\n";
print $regex->match("xyz")->from()."\n";
print "regex->match('xyz')->to:\n";
print $regex->match("xyz")->to()."\n";
print "---------------------------\n";
SKIP:
print "no match\n";
print "---------------------------\n";
print "regex->substitute: (match)\n";
print "regex->substitute() result = ";
print $regex->substitute("ci","m")->get_result()."\n";
print "---------------------------\n";
print "regex->substitute (no match):\n";
defined ($regex->substitute("ty","w")) ? () : goto SKIPPED;
print "---------------------------\n";
SKIPPED:
print "no match for substitution\n";
print "---------------------------\n";
print "regex->substitute (match):\n";
print "regex->substitute() _result = ";
print $regex->substitute("ci","x")->get_result()."\n";
print "---------------------------\n";
print "new_regex->substitute_all():\n";
my $last = "I batted 4 for 4!";
my $new_regex = Wrapper->new("4")
	or die("Error creating Wrapper object!");
print "new_regex->substitute_all() _result = ";
print $new_regex->substitute_all($last,"four")->get_result()."\n";
print "---------------------------\n";
print "regex->substitute_all() (attribute doesn't exist):\n";
print "new_regex->substitute_all() _hemans = ";
print $new_regex->substitute_all($last,"four")->get_hemans()."\n";

OUTPUT

'regex' object :
$VAR1 = bless( {
                 '_regex' => qr/cie/,
                 '_pattern' => 'cie'
               }, 'Wrapper' );
$VAR1 = qr/cie/;
---------------------------
regex->match (full):
$VAR1 = 'cie';
regex->match('cie')->from:
0
regex->match('cie')->to:
2
---------------------------
regex->match (partial):
$VAR1 = 'ci';
---------------------------
regex->match('ci')->from:
0
regex->match('ci')->to:
1
regex->match('ci')->subpatterns(): 
$VAR1 = 1;
---------------------------
regex->match (no match):
no match
---------------------------
regex->substitute: (match)
regex->substitute() result = me
---------------------------
regex->substitute (no match):
no match for substitution
---------------------------
regex->substitute (match):
regex->substitute() _result = xe
---------------------------
new_regex->substitute_all():
new_regex->substitute_all() _result = I batted four for four!
---------------------------
regex->substitute_all() (attribute doesn't exist):
new_regex->substitute_all() _hemans = Match::AUTOLOAD : Not a 'get' call or attribute doesn't exist.

Description

Small, 2-file 'module' providing practical, useful Perl regex functionality. Pattern matching, obtain 'to' and 'from' match indices, substitute part of a pattern match, and substitute all matching patterns with a new pattern.

Comments

Please sign up to post a review.