Perl Regex One-Liner To Substitute Multiline Text
Posted by w3p706 (w3p706), 11 January 2005Hello
I'd like to ask this question in the other way: Why does this work?
I'did the following:
This line peplaces
As i understand the regex, the 'AS' shouldn't be in the subtituted text, because in the second part of the regex (the replacement) 'AS' is missing.
First i tried with something like this:
Why this isn't working too?
This isn't the first time i run into trouble with multiline replacement, but every time i found a way to do it. But finally i like to know if there is a general error in my imagination of regex in multiline scenarios.
Thank You Very Much For Your Suggestions!
BTW: I'm Working with Active State Perl v5.8.2 on W2k, and Perl is acting the same if files are fomatted with unix or windows linebreaks.
Posted by Custard (Custard), 11 January 2005Hi,
I don't have time to test right now, but from your first regex, it looks like the regex is stopping at the $ so the AS gets left out of the substitution.
And you may find some mileage in the 'm' modifier on the sub ( s///m ). This specifies that $ can match end of line within a string.
(Sorry, just noticed you are using 'm' already!!!)
Also you will probably have to unset $/ to make perl ignore the line breaks.
This may not be what you want to do though. Maybe your file is big ?
I'll have a read up and get back to you on this later.
Posted by admin (Graham Ellis), 11 January 2005I'm noticing that the original post was a one-liner with the Perl command on the command line and embedded in double quotes. Now - I'm not a Windows expert, but on Linux and Unix that would cause a problem as the $AS would be treated as a shell variable.
Posted by w3p706 (w3p706), 11 January 2005I believe this is not the case here, because in windows the % sind indicates a env-var.
And in this man we have it too:
See the last paragraph of 'Using perl from the command line'
But i've seen that if i use -w then the following warnig is generated:
So Perl threat this as var, witch explains the Error.
I tryed to unset the $/ and it worked! THANK YOU!
Now I'll stick with:
Thats the magic of Perl......
Posted by Custard (Custard), 12 January 2005Just thought of something else looking at your original one-liner. AFAICR m// will interpolate variable names, so the $AS would look like a variable and would produce an error under 'use strict'.
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho