Re: Regular Expressions: How to check for these characters?



OT: If anyone is interested in a more complete list of reserved names, or
other rules for naming a file, have a glance at this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/naming_a_file.asp.
There's stuff in there that many people might not know, or might have
forgotten.

Tony Proctor

"Mike D Sutton" <EDais@xxxxxxxx> wrote in message
news:uUdoAp6aFHA.3840@xxxxxxxxxxxxxxxxxxxxxxx
> > I want to create a function in which I would be using Regular
Expressions to
> > check for the following characters:
> >
> > / \ : * ? "" < > |
> >
> > These characters are not allowed in file names, that is why I need to
> > validate if file names dont contain these characters. Currently I use
the
> > following code:
> >
> > Public Function ValidateString(ByVal sString As String) As Boolean
> > ' Validates if certain characters exist inside a string expression.
> > Dim oReg As New RegExp
> > oReg.Pattern = "[\/:*?""<>|]"
> > ValidateString = oReg.Test(sString)
> > Set oReg = Nothing
> > End Function
> >
> > Ok this function works very well. It catches all the characters except
for
> > ( \ - backslash) character. Because backslash is used as a keyword in
> > RegExp, it doesnt catch it in my function.
> >
> > How would the Pattern be so it catches the backslash too ?
>
> If you're just trying to validate a file name then here's a more complete
routine which also validates against reserved
> file names:
>
> '***
> Private Function ValidFName(ByVal inName As String, _
> Optional ByVal inQuickCheck As Boolean = False) As Boolean
> Dim DotPos As Long
> Dim TempName As String
> Dim ScanStr As Long
>
> If (Len(inName) = 0) Then Exit Function
>
> If (inQuickCheck) Then
> ValidFName = Not (inName Like "*[\/:*?!<>|""]*")
> Else
> ' Remove extension if present
> DotPos = InStr(1, inName, ".")
> If (DotPos) Then _
> TempName = UCase(Left$(inName, DotPos - 1)) Else _
> TempName = UCase(inName)
>
> Select Case TempName ' Check agaist reserved device names
> Case "CON", "PRN", "AUX", "CLOCK$", "NUL": Exit Function
> End Select
>
> If ((TempName Like "CON#") Or (TempName Like "LPT#")) Then _
> If (Mid$(TempName, 4, 1) <> "0") Then Exit Function
>
> ' File cannot end with a trailing space or a period.
> ' Although the underlying file system may support
> ' such names, the operating system does not.
> Select Case Right$(inName, 1)
> Case ".", " ": Exit Function
> End Select
>
> ' Check for any known problematic characters
> For ScanStr = 1 To Len(inName)
> Select Case Asc(Mid$(inName, ScanStr, 1))
> Case Is < 32, 33, 34, 42, 47, 58, 60, 62, 63, 92, 124:
Exit For
> End Select
> Next ScanStr
>
> ' Did we get to the end of the loop?
> ValidFName = ScanStr > Len(inName)
> End If
> End Function
> '***
>
> Hope this helps,
>
> Mike
>
>
> - Microsoft Visual Basic MVP -
> E-Mail: EDais@xxxxxxxx
> WWW: Http://EDais.mvps.org/
>
>


.