| ||||||||||||||
| ||||||||||||||
Perl Regex One-Liner To Substitute Multiline Text Posted by w3p706 (w3p706), 11 January 2005 Hello I'd like to ask this question in the other way: Why does this work? I'did the following: Code:
This line peplaces Code:
with Code:
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: Code:
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 2005 Hi,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. B Posted by admin (Graham Ellis), 11 January 2005 I'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 2005 I believe this is not the case here, because in windows the % sind indicates a env-var. And in this man we have it too: http://aspn.activestate.com/ASPN/docs/ActivePerl/lib/Pod/perlwin32.html#usage_hints_for_perl_on_win32 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: Code:
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: Code:
Thats the magic of Perl...... Posted by Custard (Custard), 12 January 2005 Just 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'.B This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| |||||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |