For Programmers: Free Programming Magazines  


Home > Archive > Tcl > August 2004 > Using lsearch to match on a string.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using lsearch to match on a string.
Derrick Cottner

2004-08-23, 4:01 pm

Hello, I am trying to use lsearch to match on a string value to check
to see if the string has any numeric elements in it.
ex. set fund_number [csubstr $msg 181 5]
set numeric [lsearch -regexp $fund_number *A-Z* ], it tells me that
quantifier operand invalid. I can not figure out what I'm doing wrong,
please help thanks.
Bryan Oakley

2004-08-23, 8:58 pm

Derrick Cottner wrote:

> Hello, I am trying to use lsearch to match on a string value to check
> to see if the string has any numeric elements in it.
> ex. set fund_number [csubstr $msg 181 5]
> set numeric [lsearch -regexp $fund_number *A-Z* ], it tells me that
> quantifier operand invalid. I can not figure out what I'm doing wrong,
> please help thanks.


Does csubstr return a list? lsearch is to be used only on lists; if you
use it on a string and that string isn't a well formed list, the command
will fail. You will also get unexpected results if the string looks
like a multi-element list rather than a single element.

But on to your question... * is a quantifier; that is, it quantifies the
subexpression before it. * means "zero or more of the preceeding
subexpression". The leading * has no preceeding subexpression which is
why you get the error.

It looks like you're thinking about glob patterns rather than regular
expressions, and for that you can use "string match". Given that you
appear to be trying to match to a string anyway that seems better way to
go than to persist in trying to make lsearch work.

Try this and see if it does what you want:

set numeric [string match {*[A-Z]*} $fund_number]

Jeff Godfrey

2004-08-23, 8:58 pm


"Bryan Oakley" <oakley@bardo.clearlight.com> wrote in message
news:uWsWc.12252$sY3.7331@newssvr22.news.prodigy.com...
: Derrick Cottner wrote:
:
: > Hello, I am trying to use lsearch to match on a string value to check
: > to see if the string has any numeric elements in it.
: > ex. set fund_number [csubstr $msg 181 5]
: > set numeric [lsearch -regexp $fund_number *A-Z* ], it tells me that
: > quantifier operand invalid. I can not figure out what I'm doing wrong,
: > please help thanks.

[ ... lots of good advice snipped ... ]

: Try this and see if it does what you want:
:
: set numeric [string match {*[A-Z]*} $fund_number]

I knew it was only a matter of time before Bryan would be all over this
one... ;^)
As usual, great advice Bryan...

Jeff


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com