Regex::Iterator - provides an iterator interface to regexps
my $string = 'string to search'; my $re = qr/[aeiou]/i; # a plain string of 'aeiou' would work as well my $it = Regex::Iterator->new($regex, $string); while (my $match = $it->match) { $it->replace('o'); } print $it->result,"\n"; # 'strong to soorch' print $it->string,"\n"; # 'string to search'
Inspired by Mark Jason Dominus' talk Programming with Iterators and Generators (available from http://perl.plover.com/yak/iterators/) this is an iterative regex matcher based on the work I did for URI::Find::Iterator
Fairly self explanatory - takes a regex and a string to match it against.
regex can be the result of a qr{}
regex
qr{}
Gets the current string we're matching against.
If a new string is optionally passed in then it will be set as the string for the iterator to match on and the iterator will be reset.
Setting returns the object itself to allow chaining.
Gets the current regex we're matching with.
If a new regex is optionally passed in then it will be set as the regex for the iterator to match with. Does not reset the iterator so you can change patterns halfway through an iteration if necessary. The regex will be automatically compiled using qr// for speed.
qr//
Returns the current match as a string.
It then advances to the next one.
Replaces the current match with replacement
Rewinds the object's state to the original string (as supplied by set_string), this allows matching to begin from the beginning again
Returns the string with all replacements.
None that I know of but there are probably loads.
Distributed under the same terms as Perl itself.
Copyright (c) 2004,
Simon Wistow <simon@thegestalt.org>
Matt Lawrence <mattlaw@cpan.org>
URI::Find::Iterator, http://perl.plover.com/yak/iterators/
To install Regex::Iterator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Regex::Iterator
CPAN shell
perl -MCPAN -e shell install Regex::Iterator
For more information on module installation, please visit the detailed CPAN module installation guide.