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

regexp and "\" (backslash)?

704 views
Skip to first unread message

Joachim Martin

unread,
Mar 22, 1995, 1:46:31 PM3/22/95
to
How do I regexp for a backslash? The following doesn't work for me:

(tclsh 7.3)
% set s "this is a \\"
this is a \

% regexp "\" $s

"
wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"
% regexp \ $s
wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"
% regexp "\\" $s
couldn't compile regular expression pattern: trailing \
% regexp \\ $s
couldn't compile regular expression pattern: trailing \


Please, someone show me the way and make me feel sheepish... -Joachim

-------------------------
Joachim Martin jma...@elmer.harvard.edu
Systems Librarian http://sirin.harvard.edu/~jmartin
Office for Information Systems Harvard University Library

Hume C.L. Smith

unread,
Mar 22, 1995, 4:39:00 PM3/22/95
to
>>>>> "JM" == Joachim Martin <jmartin@elmer> writes:

JM> How do I regexp for a backslash? The following doesn't work for me:
JM> (tclsh 7.3)
JM> % set s "this is a \\"
JM> this is a \

JM> % regexp "\" $s

JM> "
JM> wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"
JM> % regexp \ $s
JM> wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"
JM> % regexp "\\" $s
JM> couldn't compile regular expression pattern: trailing \
JM> % regexp \\ $s
JM> couldn't compile regular expression pattern: trailing \


JM> Please, someone show me the way and make me feel sheepish... -Joachim

regexp \\\\ $s
regexp {\\} $s

you need to get two backslashes (backsolidii?) into regexp - one to
escape the other in regexp meaning.

without braces, the \s have meaning to the reader, so you have to
escape each \ again - so you need four.

with braces, \s are pretty much ignored - so you can just write the
two backslashes regexp needs.

(\s count inside braces - set a {\} is considered incomplete - but
they survive in the string represented - set a {\}} sets a to \} ).

regexp is one of the nicest features of tcl - how i wish sh had it -
but it's also often painful to use, because RE syntax collides with
tcl's (e.g. brackets). sometimes you can put the RE in braces, but
often you want to use things like \t, etc. etc... the quoting hell
that's usually avoided by tcl as compared to sh, happens to reappear
in this case. perhaps there's a computer-linguistic law, of
conservation of quoting hell? :-)
--
Hume Smith hcls...@ra.isisnet.com


"Ah. Left brain! Foop." thumb

u01mtm

unread,
Apr 1, 1995, 3:00:00 AM4/1/95
to
Joachim Martin (jmartin@elmer) wrote:
: How do I regexp for a backslash? The following doesn't work for me:

: (tclsh 7.3)
: % set s "this is a \\"
: this is a \

[attempts deleted]

: Please, someone show me the way and make me feel sheepish... -Joachim

: -------------------------
: Joachim Martin jma...@elmer.harvard.edu
: Systems Librarian http://sirin.harvard.edu/~jmartin
: Office for Information Systems Harvard University Library


Try :

% set s "this is a \\"
this is a \

regexp "\\\\" $s
1


The reason the above works is because when the command line is evaluated the
"\\\\" is expanded (reduced) to "\\" (i.e. is treats it as two "\\"'s)
the "\\" is then passed to the regexp routine which then evaluates it to "\"


Hope this helps.

Mike.


Donal K. Fellows

unread,
Apr 4, 1995, 3:00:00 AM4/4/95
to
In article <3ljhst$s...@nof.abdn.ac.uk>, u01mtm <u01...@nof.abdn.ac.uk> wrote:
>Joachim Martin (jmartin@elmer) wrote:
>: How do I regexp for a backslash? The following doesn't work for me:
>
>: (tclsh 7.3)
>: % set s "this is a \\"
>: this is a \
>
>[attempts deleted]
>
>Try :

>
>regexp "\\\\" $s
>1
>
>The reason the above works is because when the command line is evaluated the
>"\\\\" is expanded (reduced) to "\\" (i.e. is treats it as two "\\"'s)
>the "\\" is then passed to the regexp routine which then evaluates it to "\"
>

You could also try using {} to quite your regexp, as in:

regexp {\\} $s
1

as this gets rid of _one_ of the layers of backslashitis...

Donal.
--
Donal K. Fellows, (at work) | Donal K. Fellows, (at home)
Dept. of Computer Science, | 6, Randall Place, Heaton,
University of Manchester | Bradford, BD9 4AE
U.K. Tel: ++44-161-275-6137 | U.K. Tel: ++44-1274-401017
fell...@cs.man.ac.uk (preferred) | do...@ugglan.demon.co.uk (if you must)
-------------------------------------------------------------------------------
See my <A HREF="http://www.cs.man.ac.uk/~fellowsd/">home page</A>
My WWW pages are now available by mail. Send mail to me with the Subject:
WWW-Mail: whatever_URL_you_want

0 new messages