Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Regular Expression problem

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2256
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Regular Expression problem
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Regular Expression problem - 3/4/2005 3:53:02 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Hi all,

Supposed I have text file containing a list of complete paths for files.

eg:

c:\folder1\folder2\folder3\file1.exe
c:\folder1\folder2\folder3\file2.exe
c:\folder1\folder2\folder3\file3.exe
c:\folder1\folder2\file1.exe
c:\folder1\folder2\file2.exe
c:\folder1\folder2\file3.exe
...
...

I'm looking for a match pattern that will match the following lines only:

c:\folder1\folder2\file1.exe
c:\folder1\folder2\file2.exe
c:\folder1\folder2\file3.exe

Basically, the idea I had was to execlude "c:\folder1\folder2\folder3\" from "c:\folder1\folder2\".

However, the pattern I made below wasn't working (matched all 6 lines isntead)

"c:\\folder1\\folder2\\\w[^\\]"

Does anyone has any idea on what would the pattern be ?

Thanks for your time.

_____________________________

Hope this helps.
 
 
Post #: 1
 
 Re: Regular Expression problem - 3/5/2005 3:52:16 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
Try the pattern

"^((c|C):\\(\w)+\\(\w)+\\(\w)+\.exe)$"

Explanation :
Start at the beginning of the string with c or C,
followed by :, followed by
three times [character \, followed by one or more
word characters], followed by
a dot, followed by exe at the end of the string.

It looks that this should work.
Good luck token !

(in reply to token)
 
 
Post #: 2
 
 Re: Regular Expression problem - 3/5/2005 8:12:16 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Thanks for the replies guys :)

tnoonan, Yes, but ^ is also used to negate a match when it is used in []

didorno, the truth is, there are other lines within the file that are not absolute path names. What I'm trying to say is that, those *other* lines could be anything. What I intend to match is basically *anything* other than the path of "C:\folder1\folder2\folder3\". Your match would definitely match the first 3 lines, but it won't match the rest of the lines (not shown) because they don't fall into that pattern.

I should applogize for my inconcised explaination. Again, what I want to match is everything except those that have "folder3" under "C:\folder1\folder2\". There could be "C:\folder1\folder2\folder4\file1.exe" in the text file and I wish to match everything except those found in a sub directory under "C:\folder1\folder2\".

Any ideas ?

Thanks.

(in reply to token)
 
 
Post #: 3
 
 Re: Regular Expression problem - 3/5/2005 11:18:02 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
tnoonan: Me and Snipah dissed you ? We are on our own ?

What are you talking about ? I didn't see Snipah's post regarding my question and the line you "quoted" was from another post. What did it has anything to do with my question ?

Did I offend you ? If so, I applogize. I have no idea what you talking about just now.

(in reply to token)
 
 
Post #: 4
 
 Re: Regular Expression problem - 3/5/2005 10:00:56 PM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
token, the only way to define exactly what you want, is by means
of the meta language string itself you are just looking for.
I think in principle it is a kind of a paradox. (The hole in the bucket song ?)

I will think it over.
Regards.

(in reply to token)
 
 
Post #: 5
 
 Re: Regular Expression problem - 3/6/2005 6:29:48 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
didorno, thanks for the reply. Well, my original ideas was pretty simple (at least it seems to me at the time).

For example, assuming I *only* want to match those files that reside in a sub-directory in "c:\folder1\folder2", I would use "c:\\folder1\\folder2\\\w+\\" and that worked.

Now, if I want to negate the last part where I only want to match those files that does NOT reside in a sub-directory in "c:\folder1\folder2", I would use "c:\\folder1\\folder2\\\w+[^\\]", because the "/" is what determines whether there is another sub directory or not. Apprently, that doesn't work as I expected.

Any ideas ?

Thanks.

(in reply to token)
 
 
Post #: 6
 
 Re: Regular Expression problem - 3/12/2005 2:09:35 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
Token, I think this is the answer :
Your pattern

      
means a match for "c:\folder1\folder2\" followed by
one or more word characters (including underscore),
followed by ONE character which is not \.

So, in your 6 example paths the matches are from c
up to and including "folder3", "folder3", "folder3", "file1.",
"file2." and "file3."
I.E. in the 1st 3 paths the matches stop before the last \, which is
according to your pattern, but
in the last 3 paths the matches stop after the dots, because
\w+ matches file1, file2, file3 and [^\\] matches the dots, also
in accordance with your pattern.

If you force the pattern to match till the end of the strings, then
you get your desired matches easily.
This works

      
and gives you even the whole desired strings by using the execute method.

Regards.

(in reply to token)
 
 
Post #: 7
 
 Re: Regular Expression problem - 3/12/2005 7:35:13 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Exactly. "c:\\folder1\\folder2\\\w+[^\\]" didn't match because for some @#$% reason, [^\\] will ALWAYS matches the character immediate before the backslash. As a result, the [^\\" will NEVER be "true" :( I wonder why that happens. =(

I was trying to avoid matching the string until the end because according to my "mind", "c:\\folder1\\folder2\\\w+[^\\]" *should* be all I needed; apprently not though. I guess the only way to get it right is to match the entire string until the very end of the string, like what you suggested. As a matter fact, "=[0-9]+$" is what is required, I just didn't mention that because it is not part of the problem and can be solved easily. =)

Anyhow, again, I think the problem have to be solved by matching the entire string like you suggested. I guess I just need to hear this from someone else that this is the only solution. =)
and I guess that's just the way regular expression works :\ I wonder who came up with the idea about regexp.

Thanks for all your help and suggestions, greately appreciated.

Thanks again.

(in reply to token)
 
 
Post #: 8
 
 Re: Regular Expression problem - 3/13/2005 12:15:02 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
quote:
Originally posted by token

Exactly. "c:\\folder1\\folder2\\\w+[^\\]" didn't match because for some @#$% reason, [^\\] will ALWAYS matches the character immediate before the backslash.


Token, no, it does what has to be done, the pattern requires at the end
one or more word characters, followed by one non \, so this is what you
get, it works correct, it has to stop before the \, because that is the non\ character.

A still less strict pattern which you can use is
      
after the third \ it requires one or more non\ till the end.

I like this kind of things, puzzling and logic.

Your welcome.

(in reply to token)
 
 
Post #: 9
 
 Re: Regular Expression problem - 3/13/2005 5:14:47 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
The file contains the following:

c:\folder1\folder2\folder3\file1.exe
c:\folder1\folder2\folder3\file2.exe
c:\folder1\folder2\folder3\file3.exe
c:\folder1\folder2\file1.exe
c:\folder1\folder2\file2.exe
c:\folder1\folder2\file3.exe
c:\folder1\folder2\folder4\file1.exe
c:\folder1\folder2\folder4\file2.exe
c:\folder1\folder2\folder4\file3.exe
c:\folder1\folder2\folder5\folder1\file1.exe
c:\folder1\folder2\folder5\folder1\file2.exe
c:\folder1\folder2\folder5\folder1\file3.exe

=======================================================

c:\\folder1\\folder2\\\w+[^\\] returned 12 lines.
c:\\folder1\\folder2\\[^\\]+ returned 12 lines.
c:\\folder1\\folder2\\[^\\]+$ returned the desired 3 lines
c:\\folder1\\folder2\\\w+[^\\]\w+ returned all 12 lines
c:\\folder1\\folder2\\\w+[^\\]\w+$ returned the desired 3 lines

What I was saying was that without the $, it will not work correctly.

Thanks

(in reply to token)
 
 
Post #: 10
 
 Re: Regular Expression problem - 3/13/2005 6:26:10 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
I see. Glad to help you Token.

Regards.

(in reply to token)
 
 
Post #: 11
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Regular Expression problem Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts