Peng Yu <pengyu...@gmail.com> asked: > I can't find an existing perl subroutine (in the library) to find > every occurrence of a substring in a string. The following webpage > "Example 3b. How to find every occurrence" uses a loop to do so. But > I'd prefer a subroutine. Could you let me know if such a subroutine is > available in perl library?
If there isn't one, just roll your own: #!/usr/bin/perl -w use strict; sub find_match { my( $text, $match, $offset ) = @_; $offset ||= 0; if( $[ - 1 < ( $offset = index( $text, $match, $offset ) ) ){ return( $offset, find_match( $text, $match, $offset+1) ); } } my $text = 'abaabaabaaabaaaaabaaabaaabaabaaaaabaabbbaabaabaaab'; my $match = 'aab'; print "matches found at: ", join(', ', find_match( $text, $match ) ), "\n"; __END__ HTH, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/