User Tools

Site Tools


k19:k19.6:k19.6.3:start

19.6.3 Excursus LIKE

The topic “Operator LIKE” from → Chapter 8.8 is taken up again here under the application aspect. It applies:

  bResult = String [NOT] LIKE Muster

and the boolean variable bResult receives the value TRUE if the pattern string matches String. If NOT is used, then the test is inverted. The following patterns can be used:

PatternDescription: Matches exactly … or is placeholder for exactly …
* A number of arbitrary characters
? A single character
[abc] One character from the character set
[x-y] One character from the given range
[^x-y] Any character that is not in the range; ^ is the negation sign here - not !
space Any number of characters with an ASCII code smaller than 32
{aaa,bbb,…} One of the 'words' in the comma-separated list

Table 19.6.3.1: Pattern for LIKE

Example 1

In a file open dialogue you can use your own file filter. A filter of the following type is often used for image files:

Dialog.Filter = [“*.png;*.jpg”, “Picture files”, “*.svg;*.wmf”, “Drawing files”].

However, with regular expressions you can set the filters in a much more sophisticated way:

  Dialog.Title = "Import a (table) text file!"
  Dialog.Filter = ["tb*.txt", "Text-Files"]      '-- The file begins with 'tb'
  Dialog.Filter = ["[0-9]*.txt", "Text-Files"]   '-- The file begins with a 'digit'
  Dialog.Filter = ["tb[1-3]*.txt; ta[1-3]*.log", "Text-Files"]

The last filter selects all files in the current directory that begin with the letter string 'tb', followed by a digit from the range 1-3, and then a sequence of arbitrary characters. The extension is either .txt or .log.

The following filter definition gives an error with Gambas 3.4.0 (FileView.CheckFilter.127: Bad regular expression:), because only one filter can be set at a time according to the definition:

  Dialog.Filter = ["tb[1-3]*.{txt,log}", "Text-Files"]

Example 2

For example, you filter page names like this:

  FOR EACH sLine IN Split(sCont, "\n")
      IF sLine LIKE "/wiki/" & CGI.Encode(Name2Wiki(TextBox2.Text)) & "*" THEN
         aUrls.Add(sLine)
      ENDIF
  NEXT

For a strong password, the requirement for at least one digit, one lowercase letter, one uppercase letter and one selected special character each could be checked with this function and the function value used for a differentiated error analysis:

  Private Function CheckStrongPassword(sPassword As String) As Integer
 
    If sPassword NOT LIKE "*[a-z]*" Then Return 1
    If sPassword NOT LIKE "*[A-Z]*" Then Return 2
    If sPassword NOT LIKE "*[0-9]*" Then Return 3
    If sPassword NOT LIKE "*[+#%]*" Then Return 4 '-- Set of characters {+ # %}
    If Len(sPassword) < 8 Then Return 5
 
    Return 0 '-- This password is a strong password
 
  End

Example 3

Assuming that the variable sDevice has the current value 'dev/ttyUSB2', no element from the pattern set matches.

  If sDevice LIKE "dev/ttyUS{B0,B1}" Then ...

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.3/start.txt · Last modified: 14.10.2023 by emma

Page Tools