NAME
Regex::Object - solves problems with global Regex variables side effects.
VERSION
version 1.24
SYNOPSIS
use
Regex::Object;
my
$re
= Regex::Object->new(
regex
=>
qr/^\w{3}$/
);
# regex to match 3 letters words
"matched\n"
if
$re
->match(
'foo'
)->success;
# prints matched
"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};
"Name: $name; Surname: $surname\n"
;
}
if
(
$result1
->success) {
my
$name
=
$result1
->named_captures->{name};
my
$surname
=
$result1
->named_captures->{surname};
"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'
);
join
"\040"
,
$matches
->match_all;
# prints John Doe Eric Lide Hans Zimmermann
DESCRIPTION
This module was created for one certain goal: give developer a level of isolation from perlre global variables.
The Regex::Object supports two approaches:
- object scoped regex
-
qr// regex passed to constructor, so these modifiers could be used: m,s,i,x,xx,p,a,d,l,u,n.
- global regex
-
collecting regex result vars from global match expression, (nothing passed to constructor).
More about Perl Regex: perlre.
Regex::Object METHODS
new(regex => $regex)
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
regex()
Returns regex that was passed to constructor earlier.
my
$regex
=
$re
->regex;
match($string)
Execute regex matching and returns Regex::Object::Match result DTO.
my
$result
=
$re
->match(
'foo'
);
match_all($string)
Execute while loop on regex with g modifier and returns Regex::Object::Matches collection.
my
$matches
=
$re
->match_all(
'John Doe Eric Lide'
);
collect($success = undef)
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.
Regex::Object::Match METHODS
success()
Returns 1 if match succeeded or '' if not.
my
$is_success
=
$result
->success;
prematch()
Returns string preceding whatever was matched by the last successful pattern match. $` equivalent.
my
$prematch
=
$result
->prematch;
match()
Returns string matched by the last successful pattern match. $& equivalent
my
$match
=
$result
->match;
postmatch()
Returns string following whatever was matched by the last successful pattern match. $' equivalent.
my
$postmatch
=
$result
->postmatch;
last_paren_match()
Returns string matched by the highest used capture group of the last successful search pattern. $+ equivalent.
my
$last_paren_match
=
$result
->last_paren_match;
captures()
Returns array ref contains of ($1, $2 ...) capture groups values.
my
$first_group
=
$result
->captures->[0];
named_captures()
Returns hash ref of the named captures. %+ equivalent.
my
$name
=
$result
->named_captures->{name};
named_captures_all()
Returns hash ref of the named captures all. %- equivalent.
my
$names_array_ref
=
$result
->named_captures_all->{name};
Regex::Object::Matches METHODS
collection()
Returns array ref with all Regex::Object::Match objects.
my
$first_match
=
$matches
->collection->[0];
count()
Returns length of the collection.
my
$count
=
$matches
->count;
match_all()
Return array ref with all matches, i.e $MATCH[].
my
$match_all_array_ref
=
$matches
->match_all;
captures_all()
Return array ref with all captures.
my
$captures_all_array_ref
=
$matches
->captures_all;
BUGS AND LIMITATIONS
If you find one, please let me know.
SOURCE CODE REPOSITORY
https://github.com/AlexP007/regex-object - fork or add pr.
AUTHOR
Alexander Panteleev <alexpan at cpan dot org>.
LICENSE AND COPYRIGHT
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.