switches with regexps

0 views
Skip to first unread message

Angelo Brigante, Jr.

unread,
Apr 16, 1999, 3:00:00 AM4/16/99
to
Hi,
I'm trying to match patterns inside a switch statement and am
having a small problem. I want to match patterns like: Num(1), Num(2),
etc. I have the following code:

switch -regexp -- $pat {
{Num\(([0-9]+)\)} { puts "Number" }
}

However, I'd also like to use regexp's "match" option to do something
like:
switch -regexp -- $pat {
{Num\(([0-9]+)\)} match a { puts "Number=$a"}
}

But this doesn't work.

How can I avoid having to do something incredibly lame like:

switch -regexp -- $pat {
{Num\(([0-9]+)\)} { regexp {Num\(([0-9]+)\)} $pat match a
puts "Number=$a" }
}


I hope this makes sense. Thanks for any tips.

--
Angelo
ang AT up-above-it DOT org


Frederic BONNET

unread,
Apr 16, 1999, 3:00:00 AM4/16/99
to
Hi Angelo,

"Angelo Brigante, Jr." wrote:
> I'm trying to match patterns inside a switch statement and am
> having a small problem. I want to match patterns like: Num(1), Num(2),
> etc. I have the following code:
>
> switch -regexp -- $pat {
> {Num\(([0-9]+)\)} { puts "Number" }
> }
>
> However, I'd also like to use regexp's "match" option to do something
> like:
> switch -regexp -- $pat {
> {Num\(([0-9]+)\)} match a { puts "Number=$a"}
> }

I suppose you want to get a mixture of switch's multi-regexps and
regexp's matchvars. Unfortunately switch doesn't allow getting matched
subexpressions values, and regexp only allows one expression at a time.

In this case you should try my tcLex extension. It makes string parsing
in Tcl much easier. Although it is rather oriented towards lexers
writing and not occasional matching, it can help you solve your problem.
It was inspired by the Unix programs lex and flex. See tcLex as a
mixture of switch, regexp and proc. In your case, you'll create a lexer,
let's call it lexNum, using a set of rules made of conditions, regexp,
match vars and script:

lexer create lexNum {
{} {Num\(([0-9]+)\)} {match a} { puts "Number=$a"}
}

(I suppose you use other rules else you wouldn't need switch).

This creates a new Tcl command named lexNum. Then call it on your
pattern:

lexNum eval $pat

For example:

lexNum eval "Num(1), Num(2)"

gives:

Number=1
Number=2

This is a rather simple case because you only have a set of rules that
apply anywhere in the string, but tcLex allows more complex cases thanks
to conditions: a rule can apply only when some conditions are met. In
your case (and in most simple cases) you don't need conditions so the
first element of each rule is empty.

You can find tcLex here:

http://www.multimania.com/fbonnet/Tcl/tcLex/index.en.htm

There is a mailing list for users support. You can also mail me for any
question, it's always a pleasure for me to hear from people who find my
work useful.

See you, Fred
--
Frédéric BONNET frederi...@ciril.fr
---------------------------------------------------------------
"Theory may inform, but Practice convinces"
George Bain

Reply all
Reply to author
Forward
0 new messages