NAME
Regexp::Flow - flow control for using regular expression
SYNOPSIS
use Regexp::Flow qw(re_matches re_substitutions);
...
my $m_results =
re_matches ( $string, $re, $code, $flags );
my $s_results =
re_substitutions ( $string, $re, $code, $flags );
....
foreach (@$m_results) {
print $_->match; # assuming you used the /p flag
}
FUNCTIONS
re_matches
my $results = re_matches ( $string, $re, $code, $flags );
my $results = re_matches ( $string, $re, $flags );
say $_->prematch for re_matches('1.23', qr/\D/p,''); #?
Finds all instances of $re
within string
and runs $code
each time a match is found. A Regexp:Flow::Result object will be created and passed as the first argument to $code
.
If $flags
is not present, g
will be assumed. If not, you must include it yourself.
If the third argument is a string, it will be used as the flags. Otherwise, it will be executed as a coderef on the Regexp::Flow::Result object, i.e. $code->($rfr)
Within $code
, you can call last
on $rfr
to stop executing $code
any more.
Note: Remember you can use any of msixpodual
on the regexp and do not need to put these in $flags
.
So, for instance, to print $1
the first time it contains a word character you could do:
my $code = sub {
my $rr = shift;
if ($rr->c(1) =~ /\w/) {
print $rr->c(1);
$rr->last;
}
}
my $string = q{'', 'a', 'b'});
re_matches ($string, qr/'([^']+)',?/, $code);
The return value of $code
is discarded (this may change).
In scalar context, the return value is a Regexp::Flow::Results object (which evaluates to the number of times a match was found, and allows access to each of the results contained within).
In void context, this value is not returned.
In list context, should it return each result?
re_substitutions
my $results = re_substitutions ( $string, $re, $code, $flags );
my $results = re_substitutions ( $string, $re, $code );
my $results = re_substitutions ( $string, $re, $string );
my $results = re_substitutions ( $string, $re );
Finds all instances of $re
within $string
and runs $code
each time a match is found. A Regexp:Flow::Result object will be created and passed as the first argument to $code
. The return value of $code
is used as the replacement for the matched string. If a string is passed as the third argument, it ($string
) will be the replcement. Therefore do not pass flags as the third argument.
Just like s///
, this makes changes to the source string, unless the r
flag is present, in which case the source string will be untouched and the return value will be the modified string.
If flags are not provided, g
is assumed.
SEE ALSO
Regexp::Result - base class for information about a regexp match
Regexp::Flow::Result - the class available within coderefs above
Regexp::Flow::Results - the list of results returned by functions above
BUGS
Please report any bugs or feature requests to the github issues tracker at https://github.com/pdl/Regexp-Result/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
AUTHORS
Daniel Perrett
LICENSE AND COPYRIGHT
Copyright 2012-2013 Daniel Perrett.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 42:
Deleting unknown formatting code M<>
- Around line 49:
Deleting unknown formatting code M<>