David Olbersen wrote:
> I'm trying to move over a string and match two types of patterns,
> call them A and B. B always matches A, and A never matches B.
>
> To work this out I decided that once I matched A on my input string,
> it would be a good idea to remove what I matched from the string.
>
> As a trivial example, I do this:
>
>     my( $a, $b, $c ) = $input =~ m/$A/o;
>     my $match = $&;

I presume $A contains some capturing parentheses to return
three values per match? I wouldn't use the /o modifier until
you find you need to optimise the program.

>     ... processing ...
>
>     $input =~ s/$match//;
>
> This works great -- but not for all cases!

It probably doesn't work when your match string contains
regex metacharacters.

    my $match = quotemeta $&;

would fix that. But using $& is a burden on Perl and to be
avoided. You could simply write

    $input =~ s/$A//;
    my( $a, $b, $c ) = ($1, $2, $3);

>
> I perform regexs in processing, but that shouldn't matter since I
> save $& directly when I want it, right?


Right. But if you need to use the original value of the string (including
the match substring) within the loop you can save it like this:

    (my $chopped = $input) =~ s/$A//;
    my( $a, $b, $c ) = ($1, $2, $3);

        ... processing ...

    $input = $chopped;

> Is anything blatantly wrong? Is there a better way to do this? Does
> anybody need more information?

I get the idea that you're wanting to find all occurrences of
either $A or $B within a target string, but avoiding matching
$B within an occurrence of $A. Am I right?

If so, you may make use of an alternation regex expression
coupled with the /g modifier on the match operator.

    while ( $input =~ m/$A|$B/g ) {
        my( $a, $b, $c ) = ($1, $2, $3);

        ... processing ...
    }

$A|$B will always match $A first if it can, while the /g will
step through the input string executing the loop body for each
match. No worries if this model doesn't fit your problem.

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to