Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Q] newline in regsub?

2,043 views
Skip to first unread message

Andreas Engel

unread,
Oct 13, 1997, 3:00:00 AM10/13/97
to

Hi!

Is it true that regsub of tcl7.5 (UNIX) is not working correct?
I want to replace all "backslash newline" expressions in a string by
"spaces":

regsub -all "\\\n" $line { } line

It replaces all "n" in the text by "Space" but the "newlines" remain.
What can I do?

Andreas!
a...@kreutler.de

: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -:
: KREUTLER Brauerstr. 50, 76135 Karlsruhe, 0721/824-0 :
: Andreas Engel a...@kreutler.de oder eng...@it.ba-karlsruhe.de :
:----------------------------------------------------------------------:

Victor Wagner

unread,
Oct 13, 1997, 3:00:00 AM10/13/97
to

Andreas Engel (andrea...@kreutler.de) wrote:
: Hi!

: Is it true that regsub of tcl7.5 (UNIX) is not working correct?
: I want to replace all "backslash newline" expressions in a string by
: "spaces":

: regsub -all "\\\n" $line { } line

: It replaces all "n" in the text by "Space" but the "newlines" remain.
: What can I do?

If you want replace COMBINATION OF TWO CHARACTERS backslash and n,
you should use either {\\n} or "\\\\n".

Note, that backslashes between quotes are first interpreted by Tcl,
and then by regsub itself. If you want newline character, you should
make Tcl prepare it, becouse \n has no special meaning for reqsub,
but tcl can convert it into actual newline character before passing
to regsub. So, in this case put JUST ONE backslash inside double
quotes.

regsub -all "\n" {This
is
a
multi
line
string
} line
works for me (plain 8.0) as expected.


: Andreas!

WANGNICK Sebastian

unread,
Oct 14, 1997, 3:00:00 AM10/14/97
to

Victor Wagner wrote:
>
> Andreas Engel (andrea...@kreutler.de) wrote:
> : Hi!
>
> : Is it true that regsub of tcl7.5 (UNIX) is not working correct?
> : I want to replace all "backslash newline" expressions in a string by
> : "spaces":
>
> : regsub -all "\\\n" $line { } line
>
> : It replaces all "n" in the text by "Space" but the "newlines" remain.
> : What can I do?
>
> If you want replace COMBINATION OF TWO CHARACTERS backslash and n,
> you should use either {\\n} or "\\\\n".
>
> Note, that backslashes between quotes are first interpreted by Tcl,
> and then by regsub itself. If you want newline character, you should
> make Tcl prepare it, becouse \n has no special meaning for reqsub,
> but tcl can convert it into actual newline character before passing
> to regsub. So, in this case put JUST ONE backslash inside double
> quotes.
>

And if you want the combination of backslash and newline,
use:

% set line
This is \
a line
% regsub -all "\\\\\n" $line " " res
1
% set res
This is a line

So you scored three out of five backslashes!

Regards,
Sebastian
--
Sebastian Wangnick <sebastian...@eurocontrol.be>
Office: Eurocontrol Maastricht UAC, Horsterweg 11, NL-6191RX Beek,
Tel: +31-433661370, Fax: ~300
Home: Lammersdorfer Str. 61, D-52159 Rott, Tel: +49-2471-2301,
Fax: ~2313, Email: familie....@t-online.de
--
To: pgp-pub...@nic.surfnet.nl, Subject: GET Sebastian Wangnick
GCS/IT d+(-) s:- a C+() U++$ P+ L+ E--- W++(--) N o? K? w++++(---)
!O !M V PS++ PE Y+ PGP?>++ t++ 5-- X- R tv--- b+++ DI? D--(+) G
e+++ h>---- r+++ y++>*

Jeffrey Hobbs

unread,
Oct 15, 1997, 3:00:00 AM10/15/97
to

In article <3441E886...@kreutler.de>,

Andreas Engel <andrea...@kreutler.de> wrote:
>I want to replace all "backslash newline" expressions in a string by "spaces":

>regsub -all "\\\n" $line { } line

You are a few slashes to short. By using the ""s, you are asking Tcl to
do one level of subsitution (which is required to get the newline), that
becomes:
\<NEWLINE>
Then the regexp also does a level of substitution, turning that simply into
a newline. You want:


regsub -all "\\\\\n" $line { } line

--
Jeffrey Hobbs "I'm really just a Tcl-bot"
jeff.hobbs at acm.org | Jeffrey.Hobbs at oen.siemens.de

0 new messages