TheItalian has asked for the wisdom of the Perl Monks concerning the following question:

hi all... i'm a new monk :D i'm writing to you now 'cause I got a trouble... i am trying to transform a plain text file into an XML one... the file looks more or less like this...
### [b]word[/b] text ###
and has to become like that:
### <TAG>word</TAG> text ###
s/\#\#\#\n\[b\](.+)\[\/b\]/<TAG>\1</TAG>/g;
but it doesnt work....seems like it doesnt match the escape character "\n"... i guess that's because windows uses the combination "end of line+cr" to "sign" the "enter" key.... so that the only "\n" doesnt actually match with anything in the text.... but... i have tried using "\r\n" or "\n\r"... but it doesnt work as well :-( any suggestion?

Replies are listed 'Best First'.
Re: \n on a TXT file
by ikegami (Patriarch) on May 27, 2009 at 22:19 UTC

    It's wrong to use \1 in the substitution expression. You need to use $1.

    Your code doesn't even compile. / in </TAG> needs to be escaped.

    Once I fix those, it works for me.

    use strict; use warnings; my $text; { local $/; $text = <DATA>; } $text =~ s/\#\#\#\n\[b\](.+)\[\/b\]/<TAG>$1<\/TAG>/g; print $text; __DATA__ ### [b]word[/b] text ###
    <TAG>word</TAG> text ###

    i guess that's because windows uses the combination "end of line+cr"

    No. When reading a file, CRLF will be converted to LF automatically on read unless you tell it otherwise.