?
Solved

Regular Expression Know-How needed

Posted on 2004-11-08
14
Medium Priority
?
184 Views
Last Modified: 2013-12-25
Hello Experts,

I'm suffering from a severe lack of Regular Expression know-how. Here's my problem: I am searching through a folder looking for specific files. Files with the extension .cck. Now the tricky part I want to exclude any file starting with 31DF1 or 31DF1\w\w\w\.cck to be precise. The filter that I came-up with on my own is
this one: "\w\w[^D]\w\w\w\w\w\.cck" and it works quite well, however I want to make it safer and more powerful. As you can see, I'm using [^D] or not D, because I'm pretty sure no other file will have a D as the third character. At first I thought using "\w\w[^D][^F]\w\w\w\w\.cck" would get me closer to my goal but after further testing it was of no value. What I really want to do is just exclude the file 31DF1\w\w\w\.\w\w\w with the same power that I have to find
31DF1\w\w\w\.\w\w\w.
Regular Expressions allow the 31DF1 in  
31DF1\w\w\w\.\w\w\w to be an AND when grouped but do not produce the same AND value between the special characters when using [^3][^1][^D][^F][^1] giving
me the same power to exclude as I have to find. Here is my code:

Filter = "\w\w[^D]\w\w\w\w\w\.cck"
ANS as Boolean
For Each file In FileCollection
ANS = TestRE(Filter, File, True)
If ANS = True Then
MsgBox ANS
End If
Next

Public Function TestRE(Filter As String, Source As String, Optional NoCase As Boolean = False) As Boolean

    Dim myRE As RegExp
    Set myRE = New RegExp
    TestRE = False
    myRE.Pattern = Filter
    myRE.IgnoreCase = NoCase
    TestRE = myRE.Test(Source)

End Function
0
Comment
Question by:dominica526
  • 9
  • 4
13 Comments
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12530851
Hi

u r actually dealing with file name , so it is better to set         NoCase = True     always
try this pattern

    Filter = "(31DF1)*\w*\.cck"

;-)
Shiju
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12530866
hi
me again
this pattern will match the occurence of ur specified patern
;-)
Shiju
0
 

Author Comment

by:dominica526
ID: 12540999
Thank you shijusn.

This is very nice, but I want to exclude that file. However in your pattern what does the () do for the 31DF1 and what does the * before the \w do?

Filter = "(31DF1)*\w*\.cck"

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12541534
hi

well u want to exculde those files with name 31df1.cck or filenames start with 31df1 isnt it ?
ok wait...

;-)
Shiju
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12541546
hi  dominica526
 try this

'----------------------------------------------------------
Private Function FindMatchFileName(ByVal sSource As String) As Boolean
Dim objRegExp As New RegExp
Dim objMatchCol As MatchCollection
    With objRegExp
        .IgnoreCase = True
        .Global = True
        .Pattern = "31DF1\w*\.cck"
    End With
    FindMatchFileName = False
    Set objMatchCol = objRegExp.Execute(sSource)
    If objMatchCol.Count > 1 Then FindMatchFileName = True
    Set objRegExp = Nothing
End Function

Private Sub Command1_Click()
    If FindMatchFileName("Your_File_name_to_check") Then
        MsgBox "Specified File Name Found"
    Else
        MsgBox "Specified File Name NOT Found"
    End If
End Sub

'------------------------------------------------------------------

;-)
Shiju
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12541563
hi
me again , sorry one modification

>>If objMatchCol.Count > 1 Then FindMatchFileName = True

change above statement to

          If objMatchCol.Count > 0 Then FindMatchFileName = True

;-)
Shiju

0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12541615
hi dominica526

>>Filter = "(31DF1)*\w*\.cck"
>>what does the * before the \w do?

   u were right there was no need for that * before \w in my first comment
only "\w+\.cck"  was enough to match any filename with cck extension
if ur file name inculdes SPACE aswell then change it to "[\w ]+\.cck"
if u want to find file name starting with "xyz" use         "xyz\w+\.cck"
if u want to find file xyz.cck or name starting with "xyz" use      "xyz\w*\.cck"

;-)
Shiju








0
 

Author Comment

by:dominica526
ID: 12541745
Shijusn.

You are basically saying with this code that if you find the file then return false instead of true. This doesn’t help my situation because I am using the FOR EACH loop to select many files with wide ranging criteria. While doing this I don’t want to pick-up a very specific file, 31DF156.cck. This pattern does the trick for me: "\w\w[^D]\w\w\w\w\w\.cck but only because so far there have been no other files with a D in the third position. I need to make a pattern that would exclude the 31DF156.cck file with including all other files.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12542063
Hi
u r saying that u want to exclude all those files with third letter as "D" or "d" ?
if my assumption is correct
u want to exclude files like
      31DF156.cck
      abd4566.cck
      34Dastdf334d.cck
      ......
      .......
in other words u want the result to be true if specified file is none of the above

;-)
Shiju
0
 

Author Comment

by:dominica526
ID: 12571828
Shiju

Your assumption is correct about my current pattern. There is just one file that I am trying to exclude 31DF1\w\w\w\.cck  with the current date being covered by \w\w\w. Any file in this folder that starts with 31DF1 needs to be excluded. My pattern "\w\w[^D]\w\w\w\w\w\.cck" works fine or at least until another file shows-up with a “D” or a “d” in the third position.  My current pattern excluded just the d, I want to make my pattern more powerful so but now I’m trying this pattern "[^3][^1][^D][^F]\w\w\w\w\.cck" and so far I can’t seem to get it to fail. The documentation I have states that [^] is used for a NOT but it only shows alpha but I think it woks with numbers as well. The problem is that the logic being applied by using [^3][^1][^D][^F] is not as a group, so I’m trying  this pattern "([^3][^1][^D][^F])\w\w\w\w\.cck" but I don’t know if it’s working.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12573104
hi
i dont understand y do u stick on to remove/exclude the pattern match instead of finding that match and skiping the situation. i would like to say that it is better to find a match for a pattern
u know rather than finding an opposite match for the pattern

to achieve ur result(get excluded result) i think
   "\w{2}[^Dd]\w+\.cck"
       
      will be enough

;-)
Shiju
 
0
 
LVL 14

Accepted Solution

by:
Shiju Sasidharan earned 1050 total points
ID: 12573138
hi
i think there is no way to find "Not operator" Result for a word or string in regular expression.
Only character by character negation is there as u tried([^3][^1][^D][^F])
;-)
Shiju
0
 

Author Comment

by:dominica526
ID: 12589545
It is easier to exclude just one file because the rest of the files will all be included. As part of the FOR EACH loop I pick-up all the files in this folder while leaving that single file. Your patter \w{2}[^Dd]\w+\.cck is the same as mine, you allow 22D489.cck to be picked up.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question