This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

matching '*' and '?' as literal chars [SOLVED]

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
TCL_no_TK
Owner
Posts: 509
Joined: Fri Aug 25, 2006 7:05 pm
Location: England, Yorkshire

matching '*' and '?' as literal chars [SOLVED]

Post by TCL_no_TK »

I've tryed using string match, but it seems to allow 'anything' as '*' (as expected). Anyone know of a way to do this, please? :)
Last edited by TCL_no_TK on Thu Oct 16, 2008 10:56 pm, edited 1 time in total.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

string match \\*\\? *?
Have you ever read "The Manual"?
User avatar
TCL_no_TK
Owner
Posts: 509
Joined: Fri Aug 25, 2006 7:05 pm
Location: England, Yorkshire

Post by TCL_no_TK »

Tryed it but got this:
[08:47:57] <Me> .tcl string match {\\*\\? *?} *!*@two*.many?.wild*.car?ds.fr
[08:47:57] <Bot> Tcl: 0
[08:48:13] <Me> .tcl string match "\\*\\? *?" "*!*@two*.many?.wild*.car?ds.fr"
[08:48:13] <Bot> Tcl: 0
[08:48:27] <Me> .tcl regexp -all -- "\\*\\? *?" "*!*@two*.many?.wild*.car?ds.fr"
[08:48:27] <Bot> Tcl: 0
[08:48:37] <Me> .tcl regexp -all -- {(\\*\\?|*?)} "*!*@two*.many?.wild*.car?ds.fr"
[08:48:37] <Bot> Tcl error: couldn't compile regular expression pattern: quantifier operand invalid
[08:48:56] <Me> .tcl string match "\\*\\? *?" "*"
[08:48:56] <Bot> Tcl: 0
[08:48:58] <Me> .tcl string match "\\*\\? *?" "*?"
[08:48:58] <Bot> Tcl: 0
[08:48:59] <Me> .tcl string match "\\*\\? *?" "* ?"
[08:48:59] <Bot> Tcl: 0
[08:49:02] <Me> .tcl string match "\\*\\? *?" "?"
[08:49:02] <Bot> Tcl: 0
[08:49:18] <Me> .tcl regexp -all -- {(\\*|\\?)} "*!*@two*.many?.wild*.car?ds.fr"
[08:49:18] <Bot> Tcl: 30
[08:49:27] <Me> .tcl regexp -all -- {(\\*|\\?)} "*!*@*.fr"
[08:49:27] <Bot> Tcl: 8
Played around a bit and tryed MC_8's filter from more tools, which dose the same thing you gave me. However, its not returning the number of matches
[09:55:04] <Me> .tcl set wd_fil1 [filter -regexp "?"]
[09:55:04] <bot> Tcl: \?
[09:55:08] <Me> .tcl set wd_fil2 [filter -regexp "*"]
[09:55:08] <bot> Tcl: \*
[09:55:26] <Me> .tcl regexp -all -- {[$wd_fil2]} "*!*@*.fr"
[09:55:26] <bot> Tcl: 1
[09:56:39] <Me> .tcl regexp -all -- {([$wd_fil1]|[$wd_fil2])} "*!*@*.fr ?"
[09:56:39] <bot> Tcl: 1
[09:56:41] <Me> .tcl regexp -all -- {([$wd_fil1]|[$wd_fil2])} "*!*@*.fr ? *"
[09:56:41] <bot> Tcl: 1
:|
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

When you add escape characters to * and ? it means they'll be treated as characters instead of wild cards. When you use "\\* \\?" as match pattern then it will only match "* ?" and nothing else (common sense). What you're probably looking for is something like

Code: Select all

string match "*\\**\\?*" $string
This will match a string containing '*' followed by '?' anywhere in a string ('*' and '?' don't necessary have to follow each other). Example
% string match "*\\**\\?*" *!*@two*.many?.wild*.car?ds.fr
1
Edit: If you want to count the number of '*' and '?' in a string then you should use [regexp] (it's not possible with [string match]). For example

Code: Select all

regexp -all {\?} $string ; # returns number of '?' in string
regexp -all {\*} $string ; # returns number of '*' in string
User avatar
TCL_no_TK
Owner
Posts: 509
Joined: Fri Aug 25, 2006 7:05 pm
Location: England, Yorkshire

Post by TCL_no_TK »

Thanks :D
Post Reply