Jump to content

Simulate DOS wildcards with a regex


Recommended Posts

I need to filter a list of files on the basis of a wildcarded DOS search criterion, for example "p*.mp?".

This will take the form of a function which accepts a filename and a wildcard-mask as inputs, and returns either true (filename is within scope of mask) or false if it is not.

Just wondered if anyone has a readymade regex to do this kind of thing, if so it would save me some head-scratching.

Link to comment
Share on other sites

This is something I recall a bunch of us playing with a while back in a discussion about creating a (pseudo) recursive _FileListToArray() function. I'm not sure this was entirely complete or debugged. Someone may have a better version around, but this may work for a starter...

$sIncludeList = "??x.*"
$File1 = "box.txt"
$File2 = "xxxx.txt"

; Convert to Regular Expression, step 1: Wrap brackets around (protect) "." and "$" (other characters needed?)
$sIncludeList = StringRegExpReplace($sIncludeList, '[.$]', '\[\0\]')
; Convert to Regular Expression, step 2: Convert '?' to '.', and '*' to '.*?'
$sIncludeList = StringReplace(StringReplace($sIncludeList, "?", "."), "*", ".*?")
; Convert to Regular Expression, step 3; make case-insensitive, match from first char, terminate strings
$sIncludeList = "(?i)\A(" & $sIncludeList & "$)"

MsgBox(1,"",$sIncludeList)

If StringRegExp($File1, $sIncludeList) Then MsgBox(1,"","File1 matches")
If StringRegExp($File2, $sIncludeList) Then MsgBox(1,"","File2 matches")
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...