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

Can I use a variable for a regexp or other pattern?

2,841 views
Skip to first unread message

Fritz Wuehler

unread,
Mar 26, 2012, 11:36:33 AM3/26/12
to
Can I use a variable for a regexp pattern or other pattern? For example

regexp {[a-z]\.txt} $string filename

I want to assign "[a-z]\.txt" to a variable and then do something like

regexp {$regExpression} $string filename

but I can't get it to work. I also had no joy with string match and format
etc. Is there a way to do this? It seems like it would be good practice to
assign patterns to variables instead of hardcoding them. Thank you for the
help.

Eugene

unread,
Mar 26, 2012, 11:40:37 AM3/26/12
to
Here's the output from my TkCon:

(bin) 49 % set filename blablah.txt
blablah.txt
(bin) 50 % set re {[a-z]\.txt}
[a-z]\.txt
(bin) 51 % regexp $re $filename
1
(bin) 52 %

To make things work in your case just remove brackets from around your
$regExpression variable in regex command ;)
--
Best regards, Eugene.

Eugene

unread,
Mar 26, 2012, 11:42:53 AM3/26/12
to
Completely forgot to explain the behavior - brackets just prevent your
regExpression variable from evaluation/expanding...

--
Best regards, Eugene.

Nomen Nescio

unread,
Mar 26, 2012, 5:50:09 PM3/26/12
to
I knew that but I didn't know what to do about it!

Thanks again mate :-)



























Glenn Jackman

unread,
Mar 27, 2012, 10:34:46 AM3/27/12
to
At 2012-03-26 05:50PM, "Nomen Nescio" wrote:
> Eugene <eugene....@gmail.com> wrote:
>
> > On 26.03.2012 19:40, Eugene wrote:
> > > On 26.03.2012 19:36, Fritz Wuehler wrote:
> > >> Can I use a variable for a regexp pattern or other pattern? For example
> > >>
> > >> regexp {[a-z]\.txt} $string filename
> > >>
> > >> I want to assign "[a-z]\.txt" to a variable and then do something like
> > >>
> > >> regexp {$regExpression} $string filename
> > >
> > > (bin) 51 % regexp $re $filename
> > > 1

Take note of how the command syntax appears in the man page:
http://tcl.tk/man/tcl8.5/TclCmd/regexp.htm

regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?

Note there's no quotes or braces around 'exp' -- it can be a string, a
variable reference, the result of a command, anything that is a single
word.

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous

Fritz Wuehler

unread,
Mar 27, 2012, 2:30:40 PM3/27/12
to
I have a few books on Tcl and I don't like any of them. I found using the
Tcl doc is a lot better. If I would have looked at it instead of trying to
extend the examples in the books I would have done better. The books do an
ok job of giving examples but I found they leave out many important details
and just focus on what they want to focus on. The Tcl doc is a lot more
helpful so far. regexp is a good example. The various books give examples and
even lists of atoms etc. but they don't tell you the lists they show are not
all inclusive. There is more to this but they pretend it doesn't exist.
Looking at what they show I found out they didn't discuss many things they
should have covered. I guess this is how people teach these days. They
didn't even refer the reader to the Tcl help pages. If I didn't look at the
web site I wouldn't know they even exist. If there a way to get man pages as
well? I must have whatever man pages come with Tcl but I don't know how to
access them.

Thanks for your comment.

Gerald W. Lester

unread,
Mar 27, 2012, 3:53:19 PM3/27/12
to
On 3/27/12 1:30 PM, Fritz Wuehler wrote:
> ... If there a way to get man pages as
> well? I must have whatever man pages come with Tcl but I don't know how to
> access them.
>
> Thanks for your comment.
>

If you are on a *nix box with Tcl install (including Mac OSX), try: man n <cmd>

For example: man n foreach

If you are on a Windows box, pull down the help file from ActiveState.


--
+------------------------------------------------------------------------+
| Gerald W. Lester, President, KNG Consulting LLC |
| Email: Gerald...@kng-consulting.net |
+------------------------------------------------------------------------+

Nikola

unread,
Mar 27, 2012, 5:56:30 PM3/27/12
to
On Mar 27, 3:53 pm, "Gerald W. Lester" <Gerald.Les...@KnG-
Consulting.net> wrote:
> On 3/27/12 1:30 PM, Fritz Wuehler wrote:
>
> > ...  If there a way to get man pages as
> > well? I must have whatever man pages come with Tcl but I don't know how to
> > access them.
>
> > Thanks for your comment.
>
> If you are on a *nix box with Tcl install (including Mac OSX), try: man n <cmd>
>
> For example: man n foreach
>
> If you are on a Windows box, pull down the help file from ActiveState.
>
> --
> +------------------------------------------------------------------------+
> | Gerald W. Lester, President, KNG Consulting LLC                        |
> | Email: Gerald.Les...@kng-consulting.net                                |
> +------------------------------------------------------------------------+

Or if you don't have a command prompt handy, but have internet
connection you can search for:
tcl man some_command

and typically the first hit would be a manual page you are looking
for.
For example "tcl man regexp" in google gives:
www.tcl.tk/man/tcl8.4/TclCmd/regexp.htm

Feel free to change 8.4 into 8.5 or 8.6 if you want to see the newer
version of the manual page.

Fritz Wuehler

unread,
Mar 28, 2012, 12:12:02 AM3/28/12
to
DOH!!!

Thanks! I thought I knew what the problem was but I didn't. I was setting
the variable to contain the regexp incluing the { }

Nomen Nescio

unread,
Mar 28, 2012, 4:00:38 AM3/28/12
to
It works! Thank you.

Nomen Nescio

unread,
Mar 31, 2012, 6:20:06 PM3/31/12
to
A guy from Princeton University who should have known better than to post
from googlegroups said:

> Or if you don't have a command prompt handy, but have internet connection
> you can search for:
> tcl man some_command
> and typically the first hit would be a manual page you are looking
> for.
> For example "tcl man regexp" in google gives:
> www.tcl.tk/man/tcl8.4/TclCmd/regexp.htm
>
> Feel free to change 8.4 into 8.5 or 8.6 if you want to see the newer
> version of the manual page.

Yes, I use the command prompt so Gerald's explanation works perfectly. And I
don't use google so thanks but NO THANKS! ;-) If I hadn't asked on the
sourceforge list about another thread I wouldn't have seen your post. I
didn't realize such an advanced group is using google groups! Thanks for the
help guys.

--
killfiling all google groups posts, thousands at a time
since 2008


M. Strobel

unread,
Mar 31, 2012, 7:13:27 PM3/31/12
to
Am 01.04.2012 00:20, schrieb Nomen Nescio:
cut----
> don't use google so thanks but NO THANKS! ;-) If I hadn't asked on the
> sourceforge list about another thread I wouldn't have seen your post. I
> didn't realize such an advanced group is using google groups! Thanks for the
> help guys.
>
> --
> killfiling all google groups posts, thousands at a time
> since 2008

Google groups is just the poor man's interface to Usenet. Nothing beats a real news
reader in the discussion domain.

Usenet has nothing to do with Google.

/Str.

Tibor Stolz

unread,
Apr 6, 2012, 8:26:25 AM4/6/12
to
> If you are on a *nix box with Tcl install (including Mac OSX), try: man n <cmd>
>
> For example: man n foreach

I've been using the tcl and tk man pages for ages. The call above does
not work on my systems as it is written here, because the first argument
"n" of the command refers to a section of man pages that does not exist.
I've just tested it across machines running different Linux
distributions I have direct or ssh access to, and I found that the
manpage section naming is distribution-dependent:

On Debian and Ubuntu systems:
$ man n lsearch
No manual entry for lsearch in section n
$ man 3tcl lsearch
(that works)
$ man 3t lsearch
(also works, abbreviated section name)

On a Fedora box:
$ man n lsearch
(that works)
$ man 3tcl lsearch
No manual entry for lsearch in section 3t
$ man 3t lsearch
No manual entry for lsearch in section 3tcl

Sometimes (e.g. for Tk procedures like text or checkbutton) it is
possible to leave out the section name at all, but in the case of "man
lsearch", a documentation on a C routine named "lsearch" is displayed;
you need to avoid that by specifying the Tcl manpages section.

By the way, there is no need to google each time for the HTML versions
of the Tcl/Tk manpages: Go to http://tcl.tk -> "DOCUMENTATION" tab ->
one of the 'Quick links' ("Tcl 8.6 (beta)", "Tcl 8.5", or "Tcl 8.4
Docs") [maybe bookmark the resulting page...], and you will find nice
directories of Tcl and Tk commands that will also help you find out if
there exists a (core only) widget to achieve what you want.

Best regards
Tibor Stolz

Donal K. Fellows

unread,
Apr 8, 2012, 12:49:40 PM4/8/12
to
On 06/04/2012 13:26, Tibor Stolz wrote:
> I've been using the tcl and tk man pages for ages. The call above does
> not work on my systems as it is written here, because the first argument
> "n" of the command refers to a section of man pages that does not exist.
> I've just tested it across machines running different Linux
> distributions I have direct or ssh access to, and I found that the
> manpage section naming is distribution-dependent:

Tcl itself specifies section 'n', but some distributions move it around
according to their own policies. We can't really help that. (We think of
section '3' and its derivatives as being for C API manual pages.) What I
can say is that we do try to make sure that we have good HTML builds on
www.tcl.tk/man of our manual pages. I think it's about correct for 8.6,
including having all the right cross-links between everything. From 8.5
onwards, there's examples for most Tcl commands and many Tk ones too
(though for Tk widgets you're best off checking the widget demo, to be
honest).

> By the way, there is no need to google each time for the HTML versions
> of the Tcl/Tk manpages: Go to http://tcl.tk -> "DOCUMENTATION" tab ->
> one of the 'Quick links' ("Tcl 8.6 (beta)", "Tcl 8.5", or "Tcl 8.4
> Docs") [maybe bookmark the resulting page...], and you will find nice
> directories of Tcl and Tk commands that will also help you find out if
> there exists a (core only) widget to achieve what you want.

Ah, you've found them! :-)

Donal.

Nomen Nescio

unread,
Apr 10, 2012, 1:58:45 PM4/10/12
to
"Donal K. Fellows" <donal.k...@manchester.ac.uk> wrote:

> On 06/04/2012 13:26, Tibor Stolz wrote:
> > I've been using the tcl and tk man pages for ages. The call above does
> > not work on my systems as it is written here, because the first argument
> > "n" of the command refers to a section of man pages that does not exist.
> > I've just tested it across machines running different Linux
> > distributions I have direct or ssh access to, and I found that the
> > manpage section naming is distribution-dependent:
>
> Tcl itself specifies section 'n', but some distributions move it around
> according to their own policies. We can't really help that. (We think of
> section '3' and its derivatives as being for C API manual pages.)

It works as it should on Slackware. I understand you need your own section
to avoid collisions unless you renamed the man pages like tcl_foreach. Maybe
that would have been better. Also it would help if there was some way to
find all the Tcl commands from the main tcl manpage, because man tclsh is
not very useful. Maybe at the bottom you could include a list of all the tcl
commands? Something similar to that is normally done in other man pages.

> What I can say is that we do try to make sure that we have good HTML
> builds on www.tcl.tk/man of our manual pages. I think it's about correct
> for 8.6, including having all the right cross-links between
> everything. From 8.5 onwards, there's examples for most Tcl commands and
> many Tk ones too (though for Tk widgets you're best off checking the
> widget demo, to be honest).

It is uncool to have to be online to get development doc. I'm ok with the
man pages but I very much appreciate you guys have the html tarball
available. This is the best solution. Would love to have one for tcllib as
well. Thanks alot for Tcl and the doc to all Tcl team.


Rob

unread,
Apr 11, 2012, 1:32:13 AM4/11/12
to
Nomen Nescio wrote:


>
> It is uncool to have to be online to get development doc. I'm ok with the
> man pages but I very much appreciate you guys have the html tarball
> available. This is the best solution. Would love to have one for tcllib as
> well. Thanks alot for Tcl and the doc to all Tcl team.

Using the URL below, take a look at the tar balls within the relevant
release folder. The one with html in its name contains full documentation,
including that for tcllib & tklib. The documentation is *not* in the form
of man pages but offers, at least IMHO, a more readable form of
documentation.

http://downloads.activestate.com/ActiveTcl/releases/

HTH

Rob.



Nomen Nescio

unread,
Apr 12, 2012, 3:40:58 AM4/12/12
to
Hi Rob,

I mean the tcllib hosted on sourceforge for use by Tcl scripts, not the tcl
library that C code can use to get Tcl functions. Unfortunate choice of
names for tcllib, but there we have it. As far as I know it is not included
in the tarballs you mentioned. As I said I already use and am thankful for
the one that covers Tcl Tk and the Tcl and Tk libraries.

Nomen

Rob

unread,
Apr 12, 2012, 8:30:31 AM4/12/12
to
Nomen Nescio wrote:

> Hi Rob,
>
> I mean the tcllib hosted on sourceforge for use by Tcl scripts, not the
> tcl library that C code can use to get Tcl functions. Unfortunate choice
> of names for tcllib, but there we have it. As far as I know it is not
> included in the tarballs you mentioned. As I said I already use and am
> thankful for the one that covers Tcl Tk and the Tcl and Tk libraries.
>

Nomen

The file I mentioned contains documentation for (apart from anything else)
extensions written in pure Tcl (included AFAIK, in the sourceforge Tcllib)
such as inifile, http, ftp, nntp, pop3 which I have used previously in my
own Tcl scripts as well as many others. Also includes documentation for
TkTable & Tablelist.

I *think* this is exactly what you are after.

Rob.

Fritz Wuehler

unread,
Apr 15, 2012, 11:19:05 AM4/15/12
to
> The file I mentioned contains documentation for (apart from anything else)
> extensions written in pure Tcl (included AFAIK, in the sourceforge Tcllib)
> such as inifile, http, ftp, nntp, pop3 which I have used previously in my
> own Tcl scripts as well as many others. Also includes documentation for
> TkTable & Tablelist.
>
> I *think* this is exactly what you are after.

You are right! Thanks!

Matthias Keppler

unread,
Nov 17, 2015, 11:09:54 AM11/17/15
to
i use tclkit (tcl8.4) and thats my solution to find procedure definition in a tcl-script:

set regProc {^[\s\t]*proc[\s][\w\:\']*[\s\t]}
if { [regexp -indices $regProc $line pos ] } {
..
.. do somethings
..
}

greetings - kmatze -
0 new messages