Regexp::Flow - flow control for using regular expression
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 } re_matches ( ... ) or warn 'no matches'; # which is easier than using while, which doesn't allow or/else
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.
$re
string
$code
If $flags is not present, g will be assumed. If not, you must include it yourself.
$flags
g
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)
$code->($rfr)
Within $code, you can call last on $rfr to stop executing $code any more.
last
$rfr
Note: Remember you can use any of msixpodual on the regexp and do not need to put these in $flags.
msixpodual
So, for instance, to print $1 the first time it contains a word character you could do:
$1
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?
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.
$string
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.
s///
r
If flags are not provided, g is assumed.
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
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.
Daniel Perrett
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:
Deleting unknown formatting code M<>
To install Regexp::Flow, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Regexp::Flow
CPAN shell
perl -MCPAN -e shell install Regexp::Flow
For more information on module installation, please visit the detailed CPAN module installation guide.