wine has asked for the wisdom of the Perl Monks concerning the following question:
Perhaps I'm not seeing the obvious mistake in my regexp-code, but I think I found some strange behavior of the regexpr engine:
wine@localhost]$ perl -pe 's/(")[^\1]*\1/check/g' "strings 1" "string 2" # input check #output
I hope you'll agree this is wrong. It should yield: check check. If you transscribe the expression by removing the backreferences you get the normal behavior:
wine@localhost]$ perl -pe 's/"[^"]*"/check/g' "string 1" "string 2" check check
Using $1 yields:
wine@localhost]$ perl -pe 's/(")[^$1]*$1/check/g' "string 1" "string 2" Unmatched [ before HERE mark in regex m/(")[ << HERE ^]*/ at -e line 1 +, <> line 1.
This isn't related to the ". It doesn't work with normal alpha-characters either:
wine@localhost]$ perl -pe 's/(a)[^\1]*\1/check/g' abbbba abbbbbbbba check
Using a | helps if the two side differ:
# ("|'"'"') is a complex bash escape for ("|') wine@localhost]$ perl -pe 's/("|'"'"')[^\1]*\1/check/g' "aaa" 'aaa' check check
But if you use differt input it goes wrong again, and this should really not happen:
perl -pe 's/("|'"'"')[^\1]*\1/check/g' "aaa" "aaa" check
Can someone explain this behavior. (").*?\1 seems to work, but is not compliant with lets say sed, which seems to have the same problem:
wine@localhost]$ sed 's/\(a\)[^\1]*\1/check/g' abbbba abbbbbbbba check
Thank you in advance
wine
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Strange regexp behavior
by Chmrr (Vicar) on Mar 26, 2002 at 12:50 UTC | |
by RMGir (Prior) on Mar 26, 2002 at 13:39 UTC | |
by Chmrr (Vicar) on Mar 26, 2002 at 13:42 UTC | |
by Fletch (Bishop) on Mar 26, 2002 at 15:04 UTC | |
by RMGir (Prior) on Mar 26, 2002 at 15:10 UTC | |
Re: Strange regexp behavior
by jepri (Parson) on Mar 26, 2002 at 12:52 UTC | |
by wine (Scribe) on Mar 26, 2002 at 13:02 UTC | |
Re: Strange regexp behavior
by RMGir (Prior) on Mar 26, 2002 at 12:55 UTC | |
Re: Strange regexp behavior
by pizza_milkshake (Monk) on Mar 26, 2002 at 21:24 UTC | |
by aersoy (Scribe) on Mar 26, 2002 at 22:15 UTC |