I am also attempting to move the position with the regex by assigning to pos(). While the assignment seems to work, as in there are no errors, it seems to have no affect.
Yes, this is documented in pos:
pos directly accesses the location used by the regexp engine to store the offset, so assigning to pos will change that offset, and so will also influence the \G zero-width assertion in regular expressions. Both of these effects take place for the next match, so you can't affect the position with pos during the current match, such as in (?{pos() = 5}) or s//pos() = 5/e.
As for the general approach, I think you should probably anchor the match to the end of the previous one with \G, use the /gc modifiers, and check for whether the end of string was reached:
my $string = " foo bar quz "; pos($string)=undef; # just to play it safe while ( $string =~ / \G \s* (foo|bar|quz) \s* /xgc) { print "<$1>\n"; } die "match failed at pos ".pos($string) unless pos($string)==length($string);
Although of course a regex is probably not the right tool here, unpack is probably better.
In reply to Re: Modifying pos() from within (?{...})
by haukex
in thread Modifying pos() from within (?{...})
by mxb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |