NAME

Regex::Object - solves problems with global Regex variables side effects.

VERSION

version 1.00

SYNOPSIS

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

######## ---- ########

$re = Regex::Object->new(regex  => qr/(?<name>\w+?) (?<surname>\w+)/); # named captures

my $result1 = $re->match('John Doe');
my $result2 = $re->match('Fill Anselmo');

# The main goal - both results have different named captured hashes
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";
}

DESCRIPTION

This module was created for one certain goal: give you a level of isolation from perlre global variables.

The Regex::Object supports qr// regex, so these modifiers could be used: m,s,i,x,xx,p,a,d,l,u,n.

More about Perl Regex: perlre.

Regex::Object METHODS

new(regex => $regex)

my $re = Regex::Object->new(regex  => qr/^\w{3}$/);

Constructor: accept one parameter - qr// regex and returns new instance.

regex()

my $regex = $re->regex;

Returns regex that was passed to constructor earlier.

match($string)

my $result = $re->match('foo');

Execute regex matching and returns Regex::Object::Match result DTO.

Regex::Object::Match METHODS

success()

my $is_success = $result->success;

Returns 1 if match succeeded or '' if not.

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};

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.