Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:43 28 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Regular Expressions

     Page 1 of 2    
Author Message
damos
Regular Member

Joined: 15/04/2016
Location: Australia
Posts: 63
Posted: 05:35am 16 Jun 2023
Copy link to clipboard 
Print this post

Am I the only twisted person here who uses regular expressions? For the sort of low level parsing stuff I tend to do, they are very handy.

I am not sure how big the C regular expression libraries are, but if they aren't too big, could this functionality be added to Webmites as they have a fair amount of space?

I don't just use them for pattern matching but also for extracting the values, so for a sequence like this:

Test string: relay1State=1&relay2State=0
Regular expression: relay(\d+)State=(\d+)

Matches:
1 values 1,1
2 values 2,0
 
LeoNicolas

Guru

Joined: 07/10/2020
Location: Canada
Posts: 479
Posted: 06:08am 16 Jun 2023
Copy link to clipboard 
Print this post

I use regex all the time. They are pretty handy for dealing with string matching and replacing. I'm not sure if the library can be incorported to the webmite.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 06:33am 16 Jun 2023
Copy link to clipboard 
Print this post

Every time I come across them I have to dive for a reference book or Google. Not something I've ever used and certainly never come across in any versions of BASIC. As far as I'm concerned they belong in C land and can stay there. :) BASIC has its own methods of comparison and searching.
Edited 2023-06-16 16:34 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3804
Posted: 06:39am 16 Jun 2023
Copy link to clipboard 
Print this post

Maybe best in a CSUB?

Some info from duckduckgo site:https://www.thebackshed.com csub pico

John
Edited 2023-06-16 17:12 by JohnS
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2076
Posted: 01:34pm 16 Jun 2023
Copy link to clipboard 
Print this post

not a complete implementation

http://www.fruitoftheshed.com/MMBasic.Regular-Expression-Function.ashx
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9129
Posted: 03:18pm 16 Jun 2023
Copy link to clipboard 
Print this post

Have a play with this (PicoMite)


PicoMite (2).zip


function

REGEX(string$, regularexpression$)

gives error if the regular expression won't compile or the match fails
gives 1 for a match
gives 0 for no match

Let me know if it works at all/nearly/well etc.
 
hhtg1968
Senior Member

Joined: 25/05/2023
Location: Germany
Posts: 123
Posted: 07:03am 17 Jun 2023
Copy link to clipboard 
Print this post

does exist a list of used "special" chars for regex?

i know from unix and cp/m *.com, test???.tx? and so on...
 
hhtg1968
Senior Member

Joined: 25/05/2023
Location: Germany
Posts: 123
Posted: 07:09am 17 Jun 2023
Copy link to clipboard 
Print this post

perhaps here?:

http://www.fruitoftheshed.com/MMBasic.Regular-Expression-Function.ashx

(i hope the url is correct). the "add hyperlink" function is to complicated for me...
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 07:51am 17 Jun 2023
Copy link to clipboard 
Print this post

As that link shows, regular expressions are already available to anyone who wants them if they cut & paste. No need to add anything to the MMBasic interpreter. ;)

[/Mr Grumpy]


(To add a hyperlink first copy the url from the page to be inked into the clipboard. I right click on the url, Select All, right click on it again, Copy. Now, in the Edit box click the hyperlink button (Earth with a chain link). You get a popup. Type into that the words that you want the viewer to see on the screen e.g. "This Link" and press OK. You get a second popup. Simply paste the clipboard contents into this. Don't delete the http at the beginning, it will all be sorted out. Press OK. A block of text will appear in your Edit window. That's the link. Just carry on typing after it as usual.)
.
Edited 2023-06-17 18:00 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
damos
Regular Member

Joined: 15/04/2016
Location: Australia
Posts: 63
Posted: 01:07am 23 Jun 2023
Copy link to clipboard 
Print this post

Thanks for the quick turn around Peter.

The BASIC example looks like it only does half the job - tells if there is a match. The other half of the job is to extract all the values.

When you use () in a regular expressions it specifies a return value is required. Normally you return an array of matches which in turn contain the return values. This is a little bit  complex for BASIC as it really doesn't handle objects, but it could just return an array of values. As per my example above, the results are:

Matches: [
["1","1"],
["2","0"]
]

This could be done as a 2-dimensional array [Matches][Value], or as single dimension array:

[ number of matches, match 1 value 1, match 1 value 2, match 2 value 1, match 2 value 2]

This gives the number of matches as return value which is indeterminate from the 2 dimensional array.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9129
Posted: 08:30am 23 Jun 2023
Copy link to clipboard 
Print this post

This was deliberate. You can emulate the same thing by looping while incrementing the start point of the search.
 
damos
Regular Member

Joined: 15/04/2016
Location: Australia
Posts: 63
Posted: 02:01am 27 Jun 2023
Copy link to clipboard 
Print this post

Hi Peter,

I finally got a chance to test the REGEX expression in your experimental code. I am not  sure what library you were using but the regex capabilities are very limited.

With a test string like:

"Extract value from v122 in test string"

I can use regular expressions like:

"value"
"v123"
"v.23"
"v[1234567890][1234567890]"

But I could not use other escapes like:

"v\d\d" which is basically the same as "v[1234567890][1234567890]".

It also does not support quantifiers like +.

At this stage it is useful as a search with wildcards but a long way from regex handling.

I did what you suggested and tried operating it inside a loop to find the position. This works but is a bit slow and so it doesn't realize the performance advantages of a native function. If it returned 0 or a value greater than 0 which indicates position found, it would greatly enhance the capability.

The other suggestion if you are thinking of tinkering with this is to make the string being searched a longstring as a big use of the function will be to parse responses from web api calls.

My idea is to use the Webmite to provide RESTful APIs to hardware rather than normal web pages. Not a lot of equipment seems to go this and people seem to be using GET. With RESTful APIs the verbs are used as follows:

GET - get data from a resource
POST - add new data to a resource
PUT - update data
DELETE - remove data
PATCH - small modifications

I noticed references to a new JSON$ (not documented in the functions section of the version of the manual I have) which will greatly assist in this implementation.
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 897
Posted: 03:27am 27 Jun 2023
Copy link to clipboard 
Print this post

@damos,

This regex functionality is integrated into the INSTR and LINSTR commands in V5.07.08 beta6.

It does return the position if the string is matched, otherwise 0.
LINSTR works on longstrings.

I have played with it, the main thing with the syntax is that various operators need to be escaped.

These expressions can be used to pull the version from your example.
"v[0-9]\+"              matches 1 or more (escape the +)
"v[0-9]\{1,3\}"         match 1 to 3      (escape { and }
"v[[:digit:]]\{1,\}     match 1 or more   [:class:]

PicoMite V5.07.08 betas

Gerry
Latest F4 Latest H7
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 07:07am 27 Jun 2023
Copy link to clipboard 
Print this post

At what point did MMBasic stop being BASIC?

<confused by this lot>








')



.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 11:21am 27 Jun 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  At what point did MMBasic stop being BASIC?


With the introduction of DO ... LOOP? ;-}
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
IanRogers

Senior Member

Joined: 09/12/2022
Location: United Kingdom
Posts: 151
Posted: 01:02pm 27 Jun 2023
Copy link to clipboard 
Print this post

I was pondering this myself!!

Imagine asking Clive Sinclair  to include all these item into his basic..LOL He would have run for the hills.

Whilst Peter is up to the challenge, I'll just watch.

How much can be crammed into this lil device..
I'd give my left arm to be ambidextrous
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 01:59pm 27 Jun 2023
Copy link to clipboard 
Print this post

Clive got one thing very right IMHO:

"abcdef"(2 TO 5 )= "bcde"
"abcdef"( TO 5) = "abcdef"(1 TO 5) = "abcde"
"abcdef"(2 TO ) = "abcdef"(2 TO 6) = "bcdef"
"abcdef"( TO ) = "abcdef"(1 TO 6) = "abcdef"
"abcdef"(3) = "abcdef"(3 TO 3) = "c"

Unusually powerful and elegant with none of that left$, right$, mid$ stuff.
It saved on keywords but it was decidedly (and typically) non-standard. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2129
Posted: 02:42pm 27 Jun 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  Clive got one thing very right IMHO:

"abcdef"(2 TO 5 )= "bcde"
"abcdef"( TO 5) = "abcdef"(1 TO 5) = "abcde"
"abcdef"(2 TO ) = "abcdef"(2 TO 6) = "bcdef"
"abcdef"( TO ) = "abcdef"(1 TO 6) = "abcdef"
"abcdef"(3) = "abcdef"(3 TO 3) = "c"

Unusually powerful and elegant with none of that left$, right$, mid$ stuff.
It saved on keywords but it was decidedly (and typically) non-standard. :)

True at the time. The ULA idea, also used on amstrad 464 and acorn electron really slowed things down but kept costs low. A game I wrote in asm for bbc micro was too slow to play on the acorn electron.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6798
Posted: 03:15pm 27 Jun 2023
Copy link to clipboard 
Print this post

RAM access on the electron was only half the speed of the BBC machines. It wasn't a bad machine though.

The ULA chips weren't particularly slow. In fact they had a lot in common with today's FPGAs.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
damos
Regular Member

Joined: 15/04/2016
Location: Australia
Posts: 63
Posted: 12:45am 28 Jun 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  At what point did MMBasic stop being BASIC?

<confused by this lot>








')



.


When we stopped using line numbers.

MMBASIC is a long way ahead of BASIC but has not gone down the path of VB. I have mixed feeling about this. I would really love to have objects and proper collections but need to remember that the intent is more of a scripting language for simple tasks.

The real power is the massive amount of work done by Peter and Geoff poring over endless datasheets (and sometimes just reverse engineering) to figure out all the nuances of specific bits of hardware and condense it into a single line BASIC command. This has made the hardware side trivially easy. On the Micromite I could easily limit myself to simple applications, but with the Webmite there is so much more potential that I am trying more challenging projects and then wishing for more powerful tools.

The biggest limitation is data processing. There is no database, SQL, LINQ or even collections apart from arrays. JSON is just at the early stages but not at the point of navigating complex trees, but even simple capabilities are really appreciated. This means that Webmite will still be just an endpoint that controls hardware and the business logic needs to be elsewhere, but that is perfectly fine. So I am looking at REST endpoints which can be accessed using React applications. This is a huge step forward from HTML applications controlling Micromites through serial ports.
 
     Page 1 of 2    
Print this page
© JAQ Software 2024