++ed by:

2 non-PAUSE users.

Simon Wistow
and 1 contributors


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) {
        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


new <regex> <string>

Fairly self explanatory - takes a regex and a string to match it against.

regex can be the result of a qr{}

string [ string ]

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.

re [ regex ]

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.

Setting returns the object itself to allow chaining.


Returns the current match as a string.

It then advances to the next one.

replace <replacement>

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/