Like operator not liking it

P

Poseur

Anyone have an idea why this doesn't work?
In my "Document_Open" routine, if the document name format is
a certain pattern, e.g. "1499011_1292004.doc", then I want to
run certain routines on it:
If ActiveDocument.Name Like "[0-9]@_[0-9]@.doc" Then...
' or "[0-9]{6,8}_[0-9]{6,8}.doc"

But it always yields a false.
When I just run the standard examples out of Help like:
"F" Like "[A-Z]" it comes out True.
If I separate it out into variables:

strDocName = ActiveDocument.Name '"1499011_1292004.doc"
boolResult = strDocName Like "[0-9]@_[0-9]@.doc"
If boolResult Then

Still false.
When I assign variables like above and watch the locals,
Document.Name is "1499011_1292004.doc" not "C:\Folder\1499011_
1292004.doc"
I've tried escaping the "_" just in case even tho it is not a
special character in this system: "\_" or "[_]" or even "[\_]"

What am I missing here?
 
S

Sabaka

Hi Poseur,

Your subject title is very clever!

For a document named "1499011_1292004.doc", the following returns a True.

Sub Similar()
If ActiveDocument.Name Like "#######_#######.doc" Then
MsgBox "true"
Else
MsgBox "false"
End If

End Sub
 
G

Greg Maxey

Poseur,

I only have my head about 3/4 around this Like function. I know two things.
It doesn't work like the wildcard strings in Find and Replace and it is
extremely frustrating.

"F" Like "[A-Z]" it comes out True because "a character" in the string is in
the range A-Z, not all characters as one would expect.

There are no @ or { } in your string so the return is false. There is
apparently no @ one or more like the preceeeding or { } feature in the Like
statement similiar to find and replace.

Try this. It isn't perfect, but it returns true for the example you gave.

Example
Sub Test()
Dim myString As String
Dim Result As String

myString = "1490212_456789.doc"
Result = Not myString Like "*[!0-9_]*.doc"

MsgBox Result
End Sub
 
G

Greg Maxey

Sabaka,

Great suggestion. I considered this as well but thought Poseur was wanting
a True return for file names with fewer or more numerical characters in the
string.

--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support
Hi Poseur,

Your subject title is very clever!

For a document named "1499011_1292004.doc", the following returns a
True.

Sub Similar()
If ActiveDocument.Name Like "#######_#######.doc" Then
MsgBox "true"
Else
MsgBox "false"
End If

End Sub



Poseur said:
Anyone have an idea why this doesn't work?
In my "Document_Open" routine, if the document name format is
a certain pattern, e.g. "1499011_1292004.doc", then I want to
run certain routines on it:
If ActiveDocument.Name Like "[0-9]@_[0-9]@.doc" Then...
' or "[0-9]{6,8}_[0-9]{6,8}.doc"

But it always yields a false.
When I just run the standard examples out of Help like:
"F" Like "[A-Z]" it comes out True.
If I separate it out into variables:

strDocName = ActiveDocument.Name '"1499011_1292004.doc"
boolResult = strDocName Like "[0-9]@_[0-9]@.doc"
If boolResult Then

Still false.
When I assign variables like above and watch the locals,
Document.Name is "1499011_1292004.doc" not "C:\Folder\1499011_
1292004.doc"
I've tried escaping the "_" just in case even tho it is not a
special character in this system: "\_" or "[_]" or even "[\_]"

What am I missing here?
 
H

Helmut Weber

Hi Greg,
It isn't perfect,

but pretty hard to show, that you are right, like

myString = "2_.doc"
myString = "_.doc"
myString = "1_1_6.doc"
myString = "_2.doc"

In linguistics, a set of rules, that define
allowed sequences of characters, is called a "grammar".
I a very simplified way. ;-)

For cases like this one, I built my own little grammar,
in pseudocode:

false if right(4) <> ".doc"
' set the starting part to a variable s
false if in s there isn't exactly 1 underscore
false if the underscore is at the leftmost position of s
false if the underscore is at the rightmost position of s
etc. etc.

Sure, the problem is solvable. But that's no challenge.
The challenge might be:
a) What is the most effective algorithm regarding speed?
Impossible to anwer, whithout knowing about the most often
occurring errors!
b) What is the shortest way of coding?
I love short code. But whether it always leads to faster execution,
I don't know. And whether it leads to better understanding, would be
another question.

Great fun twisting brains!

Greetings from Bavaria, Germany

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

Greg

Helmut,

Nearer to perfect but far from pretty:

Sub Test()
Dim myString As String
Dim intResult As Boolean
Dim Result As Boolean
Dim i As Long
Dim j As Long
myString = "14_9012456789.doc"
i = Len(myString)
intResult = Not myString Like "*[!0-9_]*.doc"
myString = Left(myString, i)
j = InStr(myString, "_")
Result = j <> 0
Result = InStr(j + 1, myString, "_") = 0 And j > 1 And j < i And
intResult

MsgBox Result
End Sub
 
S

Sabaka

Hi Greg,

You're right Poseur may be looking for names with more or fewer numbers, but
his question refers to a format is that is "a certain pattern, e.g.
'1499011_1292004.doc'" (Poseur does not say that the format is "one of
several possible patterns). I thought that if Poseur is referring to more
than one possible format - and I agree he may be - he could clarify his
question. Assuming there is more than one format, knowing what those formats
are might suggest other solutions. For instance, if there are only a couple
formats, maybe one could AND together they required number of Like
comparisons. Is the "_" always in a particular position? If so, maybe it
could be cropped out along with the ".doc" and one could test to see if what
is left is numeric.

Greg Maxey said:
Sabaka,

Great suggestion. I considered this as well but thought Poseur was wanting
a True return for file names with fewer or more numerical characters in the
string.

--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support
Hi Poseur,

Your subject title is very clever!

For a document named "1499011_1292004.doc", the following returns a
True.

Sub Similar()
If ActiveDocument.Name Like "#######_#######.doc" Then
MsgBox "true"
Else
MsgBox "false"
End If

End Sub



Poseur said:
Anyone have an idea why this doesn't work?
In my "Document_Open" routine, if the document name format is
a certain pattern, e.g. "1499011_1292004.doc", then I want to
run certain routines on it:
If ActiveDocument.Name Like "[0-9]@_[0-9]@.doc" Then...
' or "[0-9]{6,8}_[0-9]{6,8}.doc"

But it always yields a false.
When I just run the standard examples out of Help like:
"F" Like "[A-Z]" it comes out True.
If I separate it out into variables:

strDocName = ActiveDocument.Name '"1499011_1292004.doc"
boolResult = strDocName Like "[0-9]@_[0-9]@.doc"
If boolResult Then

Still false.
When I assign variables like above and watch the locals,
Document.Name is "1499011_1292004.doc" not "C:\Folder\1499011_
1292004.doc"
I've tried escaping the "_" just in case even tho it is not a
special character in this system: "\_" or "[_]" or even "[\_]"

What am I missing here?
 
G

Greg Maxey

Helmut,

Yep, I added the lcase

B = Not Left(myString, i - 4) Like "*[!0-9_]*"



My understanding is: "If" any character in the sting is "not" (signified by
the "!") in the range

0-9 or a _ then the Like statement is true.



e.g.,



B = 1234A Like "*[!0-9_]*" = True ergo

B = Not 1234A Like "*[!0-9_]*" = False



B = 1234 Like "*[!0-9_]" = False because no single character in the string
is not in the range

0-9_ ergo

B = Not 1234 Like "*[!0-9_]" = True



Similarly,



B = A123456 Like "*[A-Z]*" = True because at least one character in the
string is in the range.



That is my current understanding. I admit that I only have my head about
3/4 around this idea at present.
 
G

Greg

Poseur,

I managed to get your method to work, but I am in a fog as to how it
works. What is revCase and where would a person go to learn how to
apply this reference tool?

Thanks.
 
J

Jeff

In that case don't use it! You or your successor may need to modify it in
the future and then you'll be stuffed.

If I seem to be ranting about this point it's because I cut my teeth in
programming using a nightmare matrix language (IIRC) called "APL"
The only way to modify many of the lines of code was to rewrite them because
they were personal "idioms" of their original creators
 
P

Poseur

Poseur,

I managed to get your method to work, but I am in a fog as
to how it works. What is revCase and where would a person
go to learn how to apply this reference tool?

Thanks.

Error. Sorry. I was dummying it out but left a business rule
specific name in there. Should be:

Function checkName(nm As String, ptrn As String) As Boolean
Dim rgX As VBScript_RegExp_55.RegExp
Set rgX = New VBScript_RegExp_55.RegExp
With rgX
.Pattern = ptrn
checkName = .Test(nm)
End With
End Function

As for using the RegExp object in general, it's outlined in the
VBScript help file, VBScrip5.chm.
Regular expressions in general, I suspect you know. If not, many
references out there.
 
P

Poseur

(e-mail address removed) (Jeff) wrote in
In that case don't use it! You or your successor may need
to modify it in the future and then you'll be stuffed.

If I seem to be ranting about this point it's because I cut
my teeth in programming using a nightmare matrix language
(IIRC) called "APL" The only way to modify many of the
lines of code was to rewrite them because they were
personal "idioms" of their original creators
You must be about my age. My CS 101 course was in APL, 1973.
That was when you had to enter those IBM punchcards in a remote
terminal and turnaround was anywhere from 5 minutes to 3 hours.
 
J

Jeff

Yes and when we had a single line on-screen editor we thought we were in
heaven! I remember having to type two characters on the keyboard to create
one character of APL. (I'm 50 BTW)
 
P

Poseur

Poseur,

I managed to get your method to work, but I am in a fog as
to how it works. What is revCase and where would a person
go to learn how to apply this reference tool?

Thanks.

Again, sorry. "Revcase" is a mistake, an incomplete cleanup.
Should be:
Function checkName(nm As String, ptrn As String) As Boolean
Dim rgX As VBScript_RegExp_55.RegExp
Set rgX = New VBScript_RegExp_55.RegExp
With rgX
.Pattern = ptrn
revCase = .Test(nm)
End With
End Function
 

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