|more useful options|
search, replace and backrefrencesby Helter (Chaplain)
|on Sep 30, 2002 at 15:13 UTC||Need Help??|
Helter has asked for the wisdom of the Perl Monks concerning the following question:
I found a solution that works, but to gain more understanding (and I'm stubborn that way) I want to know why my first solution does not work.
I'm parsing a file, looking for a single line to replace 2 characters.
Line in question:
My initial search/replace was this:
This causes an error to be printed:
A co-worker suggested a change, but didn't know why it might work:
So I moved the leading and trailing underscore out of the parens and put them in the replace string.
Here are some things I think I understand, but then again maybe not...
1. Backrefrences are 1-9, only a single digit.
2. Underscores are valid variable name components.
So what changes between these two examples? If the first variable is being treated as $10_0, then why is the second not being treated as $1_0_0_?
This solution also works:
But it is probably slower than the other working solution.
Thanks for the enlightenment!