Simon Wistow > Regex-Iterator > Regex::Iterator

Download:
Regex-Iterator-0.3.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 0.3   Source  

NAME ^

Regex::Iterator - provides an iterator interface to regexps

SYNOPSIS ^

        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'

DESCRIPTION ^

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

METHODS ^

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.

match

Returns the current match as a string.

It then advances to the next one.

replace <replacement>

Replaces the current match with replacement

rewind

Rewinds the object's state to the original string (as supplied by set_string), this allows matching to begin from the beginning again

result

Returns the string with all replacements.

BUGS ^

None that I know of but there are probably loads.

COPYING ^

Distributed under the same terms as Perl itself.

AUTHOR ^

Copyright (c) 2004,

Simon Wistow <simon@thegestalt.org>

Matt Lawrence <mattlaw@cpan.org>

SEE ALSO ^

URI::Find::Iterator, http://perl.plover.com/yak/iterators/