19.6.4 Checking whether certain characters are present in a string

The following three procedures can be used to check whether at least one character from a given character set is present in a given character string. The procedures are used, for example, when checking passwords if a strong password is required.

In my opinion, a strong password can be defined like this:

  Public Function Match(Subject As String, Pattern As String) As Boolean
    Dim rRegex As Regexp
 
    rRegex = New Regexp(Subject, Pattern)
 
    If rRegex.Offset = -1 Then
       Return False
    Else
       Return True
    Endif '-- rRegex.Offset = -1 → no match ?
 
  End

and used for the check:

  Private Function CheckStrongPassword(sPassword As String) As Boolean
    Dim sSubject, sPattern As String
 
    sSubject = sPassword
    sPattern = "(?=^.{8,}$)(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?![.\n])(?=.*[+#_@!?§$%*]).*$"
 
    If Match(sSubject, sPattern) = True Then
       Return True
    Else
       Return False
    Endif
  End

Similarly, it can be determined whether the optionally used prefix has been correctly specified when encoding a string according to the DES algorithm. The prefix is exactly two characters long and contains digits or small letters or capital letters or a special character from the set {./}:

  Private Function CheckPrefix(sPrefix As String) As Boolean
    Dim sSubject, sPattern As String
'-- sPrefix character set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
 
    sSubject = sPrefix
    sPattern = "^([a-zA-Z0-9./]{2})$" ' ** Test pattern
 
    If Match(sSubject, sPattern) = True Then
       Return True
    Else
       Return False
    Endif
  End

The development of regular expressions is not a content of the chapter. Only tested patterns from own Gambas projects are included. The regular expression in ** is still simple - say those who have studied regular expressions intensively. Here comes the proof:

Download