Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

issue with STRING MATCH [some -range]

Status
Not open for further replies.

johnlopez2000

Programmer
Aug 2, 2002
90
US
I am attempting to use to create a function that will let me determine if there:

-hasalpha
-hasnumeric

properties inthe string.

This would be used to test a string for "purity", ie: determining if a string has mixed alpha-numeric chars.

I thought I could use STRING MATCH with a range of [0-9] to test for the presence of numeric chars, and STRING MATCH with a range of [A-Z] to test for the presence of some alpha chars. Have done similar with GREP.

However, when I attempt to use the braketed range:

% set x [string match [0-9] "0000"]
invalid command name "0-9"

TCL interprets the [] as containing an internal command. The TCL man page says:

string match pattern string
...
[chars] Matches any character in the set given
by chars. If a sequence of the form x-y
appears in chars, then any character
between x and y, inclusive, will match.

I've also attempted to use the SUBST command to overcome this:

set x [subst -nocommands {string match [0-9] "0000"}]

but all permutations that I've tried to then exec the above yield the same error.

Your suggestions are most appreciated.
John Lopez
Enterprise PDM & Integration Consulting
Development for MatrixOne PDM Systems
johnlopez2000@hotmail.com
 
same issue with regexp. the [0-9] get interpreted as an embeded command rather than and yields the same error.

thanks
John Lopez
Enterprise PDM & Integration Consulting
Development for MatrixOne PDM Systems
johnlopez2000@hotmail.com
 
You have to escape the brackets of course, please
see your docs for the correct syntax.

set prog "c:\\bogus\\foo\\path\\exefile"
regexp "\[a-zA-Z\](.+)" $prog all a1
 
Or quote the pattern with {}, which prevents substitutions on the argument:

Code:
regexp {^[0-9]+$} $data match

By the way, the command:

Code:
string match {[0-9]} $value

actually tests whether the string consists of a single digit. For string match to return True, the pattern must account for all characters in the string, there can't be any "leftover" unmatched characters in the string or else it returns False.

By the way, a much easier way to accomplish this is with the string is command, introduced in Tcl 8.1.1. It tests whether a string is of a certain class. Here are some examples:

[tt]% string is digit 4123
1
% string is digit 4123c
0
% string is alpha Abracadabra
1
% string is alpha Abracadabra54321
0
% string is integer -312
1
% string is integer 0x3c
1
% string is integer foobar
0
% string is double 3.1415e7
1[/tt]

You can read more about the different classes of characters you can test for in the string reference page: - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
--marsd:

regexp "\[a-zA-Z\](.+)" $prog all a1

I had tried to escape it with: \[a-z\] but had not tried to "" it also, to be: {\[a-z\]}

--Ken:

thanks, was being dumb. {[...]} works fine.

I have looked for "string is ..." type function at TCL 8.0 but obviously does not exist at that ver. I am stuck at the default for our Matrix application which is TCL 8.0. We can configure a later version, but this is not default and this means the alternate version would have be to configured at 100's users configs. So ... not yet an option.

What I did do that worked per your recomendation:

% set x "01234b5678"
01234b5678
% string match {*[0-9]*} $x
1
% string match {*[a-z]*} $x
1
% set x "012345678"
012345678
% string match {*[a-z]*} $x
0

So, given the constraints, it should work.

Gentlemen, Thanks again for your help.

John Lopez John Lopez
Enterprise PDM & Integration Consulting
Development for MatrixOne PDM Systems
johnlopez2000@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top