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

Ampersands - the SCOURGE of TCL - Help!

23 views
Skip to first unread message

Phil Powell

unread,
Jan 4, 2002, 5:48:28 PM1/4/02
to
Consider this block of text:

set html "
<a href=http://valsignalandet.com/cgi-bin/cgiwrap/ppowell/polldisplay.cgi?maxPolls=3&startPollID=9>Click
Here for &quot;The V&#228;lsignalandet Poll&quot;</a>
<script>
if (document.all && document) alert('Hello & Welcome');
</script>
"

In the variable html I want to be able to "escape" all occurrences of
ampersands so that when rendered html will look like this:

set html "
<a href=http://valsignalandet.com/cgi-bin/cgiwrap/ppowell/polldisplay.cgi?maxPolls=3&startPollID=9>Click
Here for &amp;quot;The V&amp;#228;lsignalandet Poll&amp;quot;</a>
<script>
if (document.all && docuent) alert('Hello &amp; Welcome');
</script>
"

What I basically want to do is the following:

1. If it is & followed by alphanumeric followed by ; replace with
&amp;[a-zA-Z0-9]+;
2. If it is & alone, leave alone.
3. If it is && alone, leave alone.
4. If it is & followed by stuff that is NOT containing all
alphanumeric or ending in ; leave along (e.g.
?maxPolls=3&startPollID=9> )

Can someone help me figure out what to do? Every pattern of a regsub
I have used has produced only partial results or no results at all,
and every single instance I listed above must all occur every time.

Thanks
Phil

rand mair fheal

unread,
Jan 4, 2002, 11:43:50 PM1/4/02
to
In article <1cdca2a7.0201...@posting.google.com>,
soa...@erols.com (Phil Powell) wrote:

first of all do you realize that in
regsub pattern source {x&x} var
& will be replaced by the substring that was matched?
and to turn this off you backslash the &
regsub pattern source {x\&x} var

what happens with
regsub -all {&([a-zA-Z0-9]+;)} source {\&amp;\1} var
(\1 is replaced with substring matched by the parenthesised re)

Donal K. Fellows

unread,
Jan 7, 2002, 4:38:46 AM1/7/02
to
rand mair fheal wrote:
> regsub -all {&([a-zA-Z0-9]+;)} source {\&amp;\1} var

Or even:
regsub -all {&(?=\w+;)} $source {\&amp;} var

Donal.
--
[Seen in a discussion about automounters; names omitted to protect the guilty]
A> "I don't know. All I know is that it wasn't there until I tried to use it"
B> "You mean like some guy's cat?"
A> "I didn't mount his cat. The photo's are fake"

Phil Powell

unread,
Jan 7, 2002, 5:37:03 PM1/7/02
to
I tried this too, also to no avail:

regsub -all {\&([a-zA-Z0-9]+;)} $text {\&amp;\1} text

Phil

mair_...@www.yahoo.com (rand mair fheal) wrote in message news:<mair_fheal-04...@c45.ppp.tsoft.com>...

Phil Powell

unread,
Jan 8, 2002, 6:16:04 AM1/8/02
to
Actually I combined both of your regsub solutions with one of my own
to get it to work, but is there somewhere where I can learn about
this? I am unclear as to WHY this works other than that it does!

regsub -all {&(?=[a-zA-Z0-9#]+;)} $stuff {\&amp;} stuff

Thanx all!
Phil

"Donal K. Fellows" <fell...@cs.man.ac.uk> wrote in message news:<3C396CA6...@cs.man.ac.uk>...

Bruce Hartweg

unread,
Jan 8, 2002, 6:37:04 AM1/8/02
to

"Phil Powell" <soa...@erols.com> wrote in message news:1cdca2a7.02010...@posting.google.com...

> Actually I combined both of your regsub solutions with one of my own
> to get it to work, but is there somewhere where I can learn about
> this? I am unclear as to WHY this works other than that it does!
>
> regsub -all {&(?=[a-zA-Z0-9#]+;)} $stuff {\&amp;} stuff
>
> Thanx all!
> Phil
>
see the re_syntax man page for details

basically the parens with ?= is a lookahead constraint - meaning
it is not part of the match, but the match (just an ampersand) is
only considered a match if followed by something that matches "[a-zA-Z0-9#]+;"
also see the wiki for more regexp stuff :

http://mini.net/tcl/986.html
http://mini.net/tcl/989.html
http://mini.net/tcl/1345.html

Bruce


Donal K. Fellows

unread,
Jan 8, 2002, 6:31:32 AM1/8/02
to
Phil Powell wrote:
> I tried this too, also to no avail:
> regsub -all {\&([a-zA-Z0-9]+;)} $text {\&amp;\1} text

Hmm... You do realize that transforming:

>>> if (document.all && document) alert('Hello & Welcome');

to:

>>> if (document.all && docuent) alert('Hello &amp; Welcome');

is completely at odds with rule 2 below?

>>> What I basically want to do is the following:
>>>
>>> 1. If it is & followed by alphanumeric followed by ; replace with
>>> &amp;[a-zA-Z0-9]+;
>>> 2. If it is & alone, leave alone.
>>> 3. If it is && alone, leave alone.
>>> 4. If it is & followed by stuff that is NOT containing all
>>> alphanumeric or ending in ; leave along (e.g.
>>> ?maxPolls=3&startPollID=9> )

Which do you want?

Donal.
-- Fools have been claiming the death of Unix since it was developed in the
late 60's. Unfortunately (for them) the death of Unix keeps getting
postponed, due to circumstances beyond their control.
-- Terry Porter <tjpo...@gronk.porter.net>

0 new messages