Author Topic: regex in QB64 using "tiny-regex-c" library  (Read 3066 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
regex in QB64 using "tiny-regex-c" library
« on: June 01, 2021, 04:45:43 pm »
I was looking for a regex library for QB64 and found one on GitHub here. I compiled the object file and was able to use it in QB64 successfully. The "re_match" function should really be the only function you need from that library. According to the GitHub page, you should be able to do most basic regex operations. I'm not too familiar with regex myself but I know some of you out there definitely are.

Code: QB64: [Select]
  1.  
  2. Declare CustomType Library ".\re" 'Make sure this is a full path to the object file.
  3.     Function regex_match& Alias "re_match" (pattern As String, text As String, Byval matchlength As _Offset)
  4.  
  5. Dim As Long match_length, match
  6.  
  7. Dim As String string_to_search: string_to_search = "It was sold for $1"
  8.  
  9. match = regex_match("\$\d", string_to_search, _Offset(match_length)) 'regex_match returns the character position (zero based) if it finds the match or -1 on failure. match_length will store the size of the string that matches the expression
  10.  
  11. If match <> -1 Then
  12.     Print Mid$(string_to_search, match + 1, match_length) 'add 1 to match since QB64 uses 1 as starting character

Another example:
Code: QB64: [Select]
  1.  
  2.     Function regex_match& Alias "re_match" (pattern As String, text As String, Byval matchlength As _Offset)
  3.  
  4. Dim As Long match_length, match
  5. Dim As String found
  6. Dim As String string_to_search: string_to_search = "I like to blow bubbles. I like to collect bobbles. My cat's name is bibbles. My infant son babbles."
  7.  
  8. match = regex_match("b[aeiou]bbles", string_to_search, _Offset(match_length))
  9.  
  10.     found = Mid$(string_to_search, match + 1, match_length)
  11.     Print found
  12.     string_to_search = Mid$(string_to_search, match + 1 + match_length)
  13.     Print string_to_search
  14.     Sleep
  15.     match = regex_match("b[aeiou]bbles", string_to_search, _Offset(match_length))
  16. Loop While match <> -1

The object file that you will need: 
And the header file: 
« Last Edit: June 02, 2021, 09:00:19 am by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: regex in QB64 using "tiny-regex-c" library
« Reply #1 on: June 02, 2021, 09:23:01 am »
My first post was incorrect as you do not only need the object file for this library. You actually need both the header and the object file. I've attached the header file to the original post.
Shuwatch!