Rob, > It probably doesn't work when your match string contains > regex metacharacters. > > my $match = quotemeta $&; > > would fix that.
You're correct, the problem was the regex meta characters, I found quotemeta and that fixed it. > 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 like this quite a bit! I had thought that in s/// you could only use $1...$n within the replacement string -- guess not! This makes much more sense to me than the other hoops I had to jump through! I never like using the reserved variables (aside from $_) if I can help it. My script made me feel dirty for looking at the internals like that! -------------------------- David Olbersen iGuard Engineer 11415 West Bernardo Court San Diego, CA 92127 1-858-676-2277 x2152 > -----Original Message----- > From: Rob Dixon [mailto:[EMAIL PROTECTED] > Sent: Tuesday, March 18, 2003 1:52 PM > To: [EMAIL PROTECTED] > Subject: Re: Removing what was matched > > > 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] > > -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]