User Tools

Site Tools


k19:k19.6:k19.6.4:start

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:

  • The password consists of at least 8 characters.
  • The password contains at least 1 capital letter.
  • The password contains at least 1 lower case letter.
  • The password contains at least 1 digit.
  • The password contains at least 1 special character from a defined character set.<x>db</x>.To work with regular expressions, the gb.pcre component must be included in the Gambas project. First the function Match(Subject As String, Pattern As String) is coded
  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:

  • ^ → Hits only if the prefix to be searched starts here.
  • () → Take the expression in the parenthesis as an element referred to by {2}.
  • [a-zA-Z0-9./] → Lower or upper case letter or digit 0..9 or character . or /
  • {2} → Exactly 2 characters from the set [a-zA-Z0-9./].
  • $ → Hits only if the prefix to be searched ends here.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k19/k19.6/k19.6.4/start.txt · Last modified: 14.10.2023 by emma

Page Tools