Author Topic: Scriptbasic: RE module  (Read 316 times)

Offline AIR

  • RETIRED
  • BASIC Developer
  • Posts: 410
Scriptbasic: RE module
« on: November 07, 2018, 11:59:37 AM »
John, you mentioned the re module crashing for you.  It's VERY particular about the syntax (code base is so old it doesn't support standard posix alias like [:word:]).

Here's an example that works:

Code: Script BASIC
  1. import re.bas
  2.  
  3. reQuery ="'([a-zA-Z]+)'.+'([a-zA-Z]+)'"
  4.  
  5. re::match("You can see in the program that the vector 'JOKER' is declared as 'global'.",reQuery)
  6.  
  7. n = re::n()
  8.  
  9. for i=1 to n
  10.   print i,". ",re::$(i)
  11.   print
  12. next i
  13.  

That query let's you extract just the words between single-quotes.

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2191
    • ScriptBasic Open Source Project
Re: Scriptbasic: RE module
« Reply #1 on: November 07, 2018, 12:18:44 PM »
Great to see the RE module actually return something useful. It is in serious need of error trapping.

Offline John

  • Forum Support / SB Dev
  • Posts: 2191
    • ScriptBasic Open Source Project
Re: Scriptbasic: RE module
« Reply #2 on: November 07, 2018, 12:30:50 PM »
It looks like PRINT alone emulates a PRINTNL directive.

Offline AIR

  • RETIRED
  • BASIC Developer
  • Posts: 410
Re: Scriptbasic: RE module
« Reply #3 on: November 07, 2018, 12:40:29 PM »
Here's an enhanced version with a single query that finds ALL single-quoted words:

Code: Script BASIC
  1. import re.bas
  2.  
  3. source = """You can see in the program that the vector 'JOKER' is declared as 'global'.
  4. Samsung has unveiled a 'flexible' display the company's 2018 developer conference in San Francisco.
  5. It's still very much in 'prototype' stage, but Samsung wanted to take the opportunity to show off
  6. how UI and UX works on a flexible display, so that 'developers' can make apps that optimize it."""
  7.  
  8.  
  9. reQuery ="'([a-zA-Z]+)'"
  10.  
  11. i=1
  12. do until re::match(source,reQuery) = 0
  13.     print i,". ",re::$(1)
  14.     source = right(source, LEN(source)-instr(source,re::$(1)))
  15.     print
  16.     i=i+1
  17. loop
  18.  

Offline John

  • Forum Support / SB Dev
  • Posts: 2191
    • ScriptBasic Open Source Project
Re: Scriptbasic: RE module
« Reply #4 on: November 07, 2018, 12:44:21 PM »
SB RE seems to be an easier regex implementation to understand. There may be hope for me after all.  ;)

RE is sort of LIKE SPLITA on steroids.
« Last Edit: November 07, 2018, 12:50:25 PM by John »

Offline AIR

  • RETIRED
  • BASIC Developer
  • Posts: 410
Re: Scriptbasic: RE module
« Reply #5 on: November 08, 2018, 03:51:00 PM »
Worked on adapting the SLRE library to SB.

Code: Script BASIC
  1. include slre.bas
  2.  
  3. source = """'Samsung' has 'unveiled' a 'flexible' display the company's 2018 developer conference in San Francisco.
  4. It's still very much in 'prototype' stage, but Samsung wanted to take the opportunity to show off
  5. how UI and UX works on a flexible display, so that 'developers' can make 'apps' that optimize it."""
  6.  
  7. regex = "'(\\S+)' "
  8.  
  9.  
  10. i=1
  11. do until slre::match(regex, source,1)  < 0
  12.     print i,". ",slre::$(0)
  13.     source = right(source, LEN(source)-instr(source,slre::$(0)))
  14.     print
  15.     i=i+1
  16. loop
  17.  
  18.  

I implemented this to follow the same syntax as the RE module.

[riveraa@MacDev ~/tmp/sb/tests] $ scriba tslre.bas
1. Samsung
2. unveiled
3. flexible
4. prototype
5. developers
6. apps


AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 2191
    • ScriptBasic Open Source Project
Re: Scriptbasic: RE module
« Reply #6 on: November 08, 2018, 04:13:04 PM »
Sweet!

Quote
SLRE is an ISO C library that implements a subset of Perl regular
expression syntax. The main features of SLRE are:

Written in strict ANSI C'89
Small size (compiled x86 code is about 5kB)
Uses little stack and does no dynamic memory allocation
Provides a simple intuitive API
Implements the most useful subset of Perl regex syntax (see below)
Easily extensible. E.g. if one wants to introduce a new metacharacter \i,
meaning "IPv4 address", it is easy to do so with SLRE.

SLRE is perfect for tasks like parsing network requests, configuration
files, user input, etc, when libraries like PCRE are too
heavyweight for the given task. Developers of embedded systems would benefit
most.

« Last Edit: November 08, 2018, 04:22:41 PM by John »