Regex::Object - solves problems with global Regex variables side effects.
version 1.24
use Regex::Object; my $re = Regex::Object->new(regex => qr/^\w{3}$/); # regex to match 3 letters words print "matched\n" if $re->match('foo')->success; # prints matched print "matched\n" if $re->match('fooz')->success; # nothing ######## ---- ######## # The main goal - both results have different named captured hashes $re = Regex::Object->new(regex => qr/(?<name>\w+?) (?<surname>\w+)/); # named captures my $result1 = $re->match('John Doe'); my $result2 = $re->match('Fill Anselmo'); if ($result2->success) { my $name = $result2->named_captures->{name}; my $surname = $result2->named_captures->{surname}; print "Name: $name; Surname: $surname\n"; } if ($result1->success) { my $name = $result1->named_captures->{name}; my $surname = $result1->named_captures->{surname}; print "Name: $name; Surname: $surname\n"; } ######## ---- ######## # Works with match regex my $re = Regex::Object->new; my $success; my @matches; while ($success = John Doe Eric Lide Hans Zimmermann' =~ /(?<name>\w+?) (?<surname>\w+)/g) { my $match = $re->collect($success); push @matches, $match; } ######## ---- ######## # Global search for scoped regex without while loop my $re = Regex::Object->new(regex => qr/([A-Z]+?) ([A-Z]+)/i); my $matches = $re->match_all('John Doe Eric Lide Hans Zimmermann'); print join "\040", $matches->match_all; # prints John Doe Eric Lide Hans Zimmermann
This module was created for one certain goal: give developer a level of isolation from perlre global variables.
The Regex::Object supports two approaches:
qr// regex passed to constructor, so these modifiers could be used: m,s,i,x,xx,p,a,d,l,u,n.
collecting regex result vars from global match expression, (nothing passed to constructor).
More about Perl Regex: perlre.
Constructor: accept one optional parameter - qr// regex and returns new instance.
my $re = Regex::Object->new(regex => qr/^\w{3}$/); # scoped qr regex my $re = Regex::Object->new; # to work with global match expression
Returns regex that was passed to constructor earlier.
my $regex = $re->regex;
Execute regex matching and returns Regex::Object::Match result DTO.
my $result = $re->match('foo');
Execute while loop on regex with g modifier and returns Regex::Object::Matches collection.
my $matches = $re->match_all('John Doe Eric Lide');
Returns Regex::Object::Match result DTO filled with values from the nearest global match expression.
my $success = $string =~ /(\w*)/ my $result = $re->collect($success);
ATTENTION!! Always pass $success, otherwise strange behavior is possible, because of built-in $MATCH saving last success.
Returns 1 if match succeeded or '' if not.
my $is_success = $result->success;
Returns string preceding whatever was matched by the last successful pattern match. $` equivalent.
my $prematch = $result->prematch;
Returns string matched by the last successful pattern match. $& equivalent
my $match = $result->match;
Returns string following whatever was matched by the last successful pattern match. $' equivalent.
my $postmatch = $result->postmatch;
Returns string matched by the highest used capture group of the last successful search pattern. $+ equivalent.
my $last_paren_match = $result->last_paren_match;
Returns array ref contains of ($1, $2 ...) capture groups values.
my $first_group = $result->captures->[0];
Returns hash ref of the named captures. %+ equivalent.
my $name = $result->named_captures->{name};
Returns hash ref of the named captures all. %- equivalent.
my $names_array_ref = $result->named_captures_all->{name};
Returns array ref with all Regex::Object::Match objects.
my $first_match = $matches->collection->[0];
Returns length of the collection.
my $count = $matches->count;
Return array ref with all matches, i.e $MATCH[].
my $match_all_array_ref = $matches->match_all;
Return array ref with all captures.
my $captures_all_array_ref = $matches->captures_all;
If you find one, please let me know.
https://github.com/AlexP007/regex-object - fork or add pr.
Alexander Panteleev <alexpan at cpan dot org>.
This software is copyright (c) 2022 by Alexander Panteleev. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Regex::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Regex::Object
CPAN shell
perl -MCPAN -e shell install Regex::Object
For more information on module installation, please visit the detailed CPAN module installation guide.