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!

TCL Regex

Status
Not open for further replies.

bebop

Programmer
Oct 15, 2002
5
US
Hi there,

I am trying to create a TCL regex that checks a string contains at least one uppercase, one lowercase and one numeric character.

I tried using [A-Z]+[a-z]+[0-9]+.* but this enforces that the first three characters have to be upper, lower, numeric.

Can anyone tell me how I can produce a regex that says there must be at least one of each somewhere within the string?

Regards

BeBop
 
This is turning out to be quite a tricky problem to solve with regular expressions. There's really no way of storing "state" information when doing regular expression parsing, except by accounting for in it the pattern that you write.

The best I've been able to come up with so far in terms of a single RE pattern isn't pretty. It uses alternation extensively, and basically accounts for all 6 permutations of "upper case," "lower case," and "digit" matches. Note that there are line breaks in this only because of Tek-Tips line length limit. This is really a single-line pattern:

Code:
set pat {[A-Z].*(?:[a-z].*\d|\d.*[a-z])|[a-z].*(?:\d.*[A-Z]|[A-Z].*\d)|\d.*(?:[A-Z].*[a-z]|[a-z].*[A-Z])}

*bleah* I used some Tcl 8.1+ RE features to make it a little shorter and a little more efficient. So you'll have to re-write it a bit if you're using it on a pre-8.1 interpreter.

Here's a few tests of it:

[tt]% regexp $pat "This is 1 test."
1
% regexp $pat "This is another"
0
% regexp $pat "another 1"
0[/tt]

I can't say as that I'm really confident enough in it to guarantee that it will work properly in all cases, but I think it would do the job.

It's going to be easier -- and probably more efficient -- if we break it down into 3 separate REs, each testing for only one thing:

Code:
if {[regexp {\d} $str]
    && [regexp {[a-z]} $str]
    && [regexp {[A-Z]} $str]} {

    puts "Valid"
} else {
    puts "Invalid"
}

That's the best I could come up with so far. There's probably some glaringly obvious solution that I'm overlooking, though... - 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
 
I have a string like "Hello$_World"
and i want to replace it as "Hello World"

I am using -

regsub -all "$\_" "Hello$_World" " " strout

But not getting the desired result.

Can someone help me.
 
You've got the "\" in front of the wrong character of your pattern. Underscore (_) has no special meaning in a regular expression, but "$" indicates that the pattern is anchored to the end of the string. So, you need to escape the "$". But "$" is also significant to the Tcl interpreter, indicating variable substitution. So to get this pattern successfully to regsub, you'll need further protection. The easiest would be something like this:

[tt]% set str {Hello$_World}
Hello$_World
% regsub -all {\$_} $str " " strout
1
% puts $strout
Hello World[/tt]

But for simple string substitutions like this, that don't take advantage of wildcards or other pattern-matching features of regular expressions, it's much simpler to use the Tcl string map command. string map accepts a list of simple string substitutions and a string to modify. (If I remember correctly, string map was added in Tcl version 8.1.1.) For example:

[tt]% string map {d c o a g t} "My dog"
My cat
% string map {cat dog} "My cat has fleas"
My dog has fleas[/tt]

So, you could just do:

[tt]% set str {Hello$_World}
Hello$_World
% [ignore]set strout [string map {$_ " "} $str][/ignore]
Hello World[/tt] - 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
 
Ken,

Thanks for the reply. I'll give it a go.

Regards

BeBop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top