Like Statement

G

Greg Maxey

Is there any way to construct a wildcard Like statement similar to the {1,}
one or more of the proceding character used in Find? Here is an example. I
want my Like statement to be true if the string is only lower case letters.
I get a a match if I use [a-z] followed by 25 asterisks. Is this statement
really limited in this area? Thanks

Sub Test()
Dim MySting As String

MyString = "abcdefghijklmnopqrstuvwxyz"

If MyString Like "[a-z]************************" Then MsgBox "Eureka"
'What I would like to do:
'If MyString Lik "[a-z]{1,}" Then ...
End Sub
 
A

Andi Mayer

Is there any way to construct a wildcard Like statement similar to the {1,}
one or more of the proceding character used in Find? Here is an example. I
want my Like statement to be true if the string is only lower case letters.
I get a a match if I use [a-z] followed by 25 asterisks. Is this statement
really limited in this area? Thanks

Sub Test()
Dim MySting As String

MyString = "abcdefghijklmnopqrstuvwxyz"

If MyString Like "[a-z]************************" Then MsgBox "Eureka"
'What I would like to do:
'If MyString Lik "[a-z]{1,}" Then ...
End Sub

I have seen a while ago a post about a regular-expression function,
but forgot if it's a programm or a dll (free or included in Windows)

But I never tried it, because I like my ASC approach much more and
have 100% control.

Sub check_aTOz(MyString as string)
Dim Nr As Long, I As Long
For I = 1 To Len(Mystring)
Nr = Asc(Mid(Mystring, I, 1))
If Nr < 97 Or Nr > 122 Then
MsgBox "Sorry the " & I & " letter is not [a-z]"
Exit For
End If
Next I
If I > Len(Mystring) Then MsgBox "Eureka"
End Sub
 
G

Greg Maxey

Thanks Andi.

--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Andi said:
Is there any way to construct a wildcard Like statement similar to
the {1,}
one or more of the proceding character used in Find? Here is an
example. I
want my Like statement to be true if the string is only lower case
letters.
I get a a match if I use [a-z] followed by 25 asterisks. Is this
statement
really limited in this area? Thanks

Sub Test()
Dim MySting As String

MyString = "abcdefghijklmnopqrstuvwxyz"

If MyString Like "[a-z]************************" Then MsgBox "Eureka"
'What I would like to do:
'If MyString Lik "[a-z]{1,}" Then ...
End Sub

I have seen a while ago a post about a regular-expression function,
but forgot if it's a programm or a dll (free or included in Windows)

But I never tried it, because I like my ASC approach much more and
have 100% control.

Sub check_aTOz(MyString as string)
Dim Nr As Long, I As Long
For I = 1 To Len(Mystring)
Nr = Asc(Mid(Mystring, I, 1))
If Nr < 97 Or Nr > 122 Then
MsgBox "Sorry the " & I & " letter is not [a-z]"
Exit For
End If
Next I
If I > Len(Mystring) Then MsgBox "Eureka"
End Sub
 
H

Helmut Weber

Hi Greg,
I want my Like statement to be true if the string is only lower case letters.
hmm...

Dim s As String
s = "abcdefghijklmnopQrstuvwxyz"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jonathan West

Helmut Weber said:
Hi Greg,
hmm...

Dim s As String
s = "abcdefghijklmnopQrstuvwxyz"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If

Hi Helmut

You might like to check the result of this...

Dim s As String
s = "1234567890-*$£^"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If


Again, the solution can be achieved using the Like operator. This function
will do the needful

Function IsLowerCase(strIn As String) As Boolean
IsLowerCase = Not strIn Like "*[!a-z]*"
End Function
 
G

Greg Maxey

Johnathan,

This certainly works, but I can't get my head around why it works when:

strIn Like "*[a-z]*" won't work.

It seems your method is saying if zero or more characters from the start or
zero or more chacters endd is not a letter a-z then the Like Statement is
false and the Not Like statement is true.

Wait a minute, perhaps the fog is clearing.

So "*[a-z]*" would mean if zero or more characters from the start or zero or
more chacters end "are" a letter a-z then the statement is true.

strIn = "abc5xyz"
So strIn Like "abc5xyz" would meet that condition and correctly (while
seemingly erroneous) return a True.

I think this how this works. If you have time would you confirm or offer
the correct explanation.

Thanks.
--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Jonathan said:
Helmut Weber said:
Hi Greg,
hmm...

Dim s As String
s = "abcdefghijklmnopQrstuvwxyz"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If

Hi Helmut

You might like to check the result of this...

Dim s As String
s = "1234567890-*$£^"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If


Again, the solution can be achieved using the Like operator. This
function will do the needful

Function IsLowerCase(strIn As String) As Boolean
IsLowerCase = Not strIn Like "*[!a-z]*"
End Function
 
J

Jonathan West

Hi Greg,

I think you didn't notice the ! character in the comparison string, or
perhaps didn't understand the significance of it. Comparing with "*[!a-z]*"
will return True if the string being checked contains any character that is
not in the range a-z. Therefore it returns False if the string is _all_
lowercase characters. Invert that result for the IsLowerCase function.

Comparing with "*[a-z]*" returns True if the string contains any character
that *is* in the range a-z. This of course will return true if the string is
mixed as well as all lowercase. My understanding is that you want a function
that return True only of all the characters are lowercase.

By the way, I didn't test the function on a zero-length string. As it
happens it returns True for a zero-length string. You'll have to decide if
that is an appropriate result - if not, you'll need to put in an extra test
for a zero-length string

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup


Greg Maxey said:
Johnathan,

This certainly works, but I can't get my head around why it works when:

strIn Like "*[a-z]*" won't work.

It seems your method is saying if zero or more characters from the start
or zero or more chacters endd is not a letter a-z then the Like Statement
is false and the Not Like statement is true.

Wait a minute, perhaps the fog is clearing.

So "*[a-z]*" would mean if zero or more characters from the start or zero
or more chacters end "are" a letter a-z then the statement is true.

strIn = "abc5xyz"
So strIn Like "abc5xyz" would meet that condition and correctly (while
seemingly erroneous) return a True.

I think this how this works. If you have time would you confirm or offer
the correct explanation.

Thanks.
--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Jonathan said:
Helmut Weber said:
Hi Greg,
I want my Like statement to be true if the string is only lower case
letters.
hmm...

Dim s As String
s = "abcdefghijklmnopQrstuvwxyz"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If

Hi Helmut

You might like to check the result of this...

Dim s As String
s = "1234567890-*$£^"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If


Again, the solution can be achieved using the Like operator. This
function will do the needful

Function IsLowerCase(strIn As String) As Boolean
IsLowerCase = Not strIn Like "*[!a-z]*"
End Function
 
G

Greg Maxey

Jonathan,

No I did see your "!" and was able to get your method to work. I was
struggling with why If MyString Like "*[a-z]*" wouldn't work.

I understand now that "*[a-z]*" means if any character in the string is in
the a-z range then the expression is true. I was thinking earlier that
"all" characters in the string would have to be in the a-z range or the
expression would be false.

Thanks for you help.

--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Jonathan said:
Hi Greg,

I think you didn't notice the ! character in the comparison string, or
perhaps didn't understand the significance of it. Comparing with
"*[!a-z]*" will return True if the string being checked contains any
character that is not in the range a-z. Therefore it returns False if
the string is _all_ lowercase characters. Invert that result for the
IsLowerCase function.
Comparing with "*[a-z]*" returns True if the string contains any
character that *is* in the range a-z. This of course will return true
if the string is mixed as well as all lowercase. My understanding is
that you want a function that return True only of all the characters
are lowercase.
By the way, I didn't test the function on a zero-length string. As it
happens it returns True for a zero-length string. You'll have to
decide if that is an appropriate result - if not, you'll need to put
in an extra test for a zero-length string


Greg Maxey said:
Johnathan,

This certainly works, but I can't get my head around why it works
when: strIn Like "*[a-z]*" won't work.

It seems your method is saying if zero or more characters from the
start or zero or more chacters endd is not a letter a-z then the
Like Statement is false and the Not Like statement is true.

Wait a minute, perhaps the fog is clearing.

So "*[a-z]*" would mean if zero or more characters from the start or
zero or more chacters end "are" a letter a-z then the statement is
true. strIn = "abc5xyz"
So strIn Like "abc5xyz" would meet that condition and correctly
(while seemingly erroneous) return a True.

I think this how this works. If you have time would you confirm or
offer the correct explanation.

Thanks.
--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Jonathan said:
Hi Greg,
I want my Like statement to be true if the string is only lower
case letters.
hmm...

Dim s As String
s = "abcdefghijklmnopQrstuvwxyz"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If

Hi Helmut

You might like to check the result of this...

Dim s As String
s = "1234567890-*$£^"
If LCase(s) = s Then
MsgBox "yes"
Else
MsgBox "no"
End If


Again, the solution can be achieved using the Like operator. This
function will do the needful

Function IsLowerCase(strIn As String) As Boolean
IsLowerCase = Not strIn Like "*[!a-z]*"
End Function
 
P

Poseur

On Thu, 13 Jan 2005 21:02:48 -0500, "Greg Maxey"


I have seen a while ago a post about a regular-expression
function, but forgot if it's a programm or a dll (free or
included in Windows)

But I never tried it, because I like my ASC approach much
more and have 100% control.
As do I now that I've seen it.
As for pre- .NET regular expressions, it is free, you need to add
the reference to the library: Microsoft VBScript Regular
Expression 5.5

Dim rgExp As VBScript_RegExp_55.RegExp
Set rgExp = New VBScript_RegExp_55.RegExp
With rgExp
.Pattern = "\swhatever\!"
.Global = True
.IgnoreCase = False
.Replace(someRange.Text, "something better")
End With

In the VBScript help file, VBSCRIP5.CHM.
Works quite well, no performance decrement I can tell (but haven't
done metrics).
Constrained by the weird .doc file format somewhat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top