Regex for LiveCode

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 529
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Regex for LiveCode

Post by kaveh1000 » Mon Sep 02, 2024 9:42 am

You're my hero Stam! OK, Seems I need to start using arrays more. And I don't think I ever used styledtext. Will go thro it all and come back with any questions.

But let us keep the pressure for regex, as there are very complex transformations that can only be done with regex. e.g. I have several hundred patterns similar to:

Code: Select all

(?<=\n)(?sx)               #Note the ?x allows me to put comments in regex
\[(.+?)\]                   
\s(.+?),\s+                 
([^,]+?),\s+                
([^,]+?)\s+                 
(\d+)                       
(?:\((\d+?)\))?             
\s+\((\d{4})\)              
 (?:\s+([^\s]+?) )?         
 (?:\s?[-–?]+\s?([^\s]+?))? 
\.(?=\n)
Very happy to be in touch and thanks again. I will be sending my regex backreferencing macros.

Regards
Kaveh
Kaveh

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 529
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Regex for LiveCode

Post by kaveh1000 » Mon Sep 02, 2024 11:46 am

Oh, and searching for StyledText I came across my own posts in this thread:
viewtopic.php?t=33764
:-)
Kaveh

stam
Posts: 2962
Joined: Sun Jun 04, 2006 9:39 pm

Re: Regex for LiveCode

Post by stam » Mon Sep 02, 2024 5:22 pm

I think you may be crediting me with Bernd's input ;)

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 529
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Regex for LiveCode

Post by kaveh1000 » Mon Sep 02, 2024 5:47 pm

No I realise it was Bernd. But clearly I had heard of styledText but never used it.

FYI you have inspired me to look again. I still have a mental block with multidimentional arrays, but I have written a loop now and the text is coloured in .5 seconds, so not too bad! Working on it...
Kaveh

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4083
Joined: Sun Jan 07, 2007 9:12 pm

Re: Regex for LiveCode

Post by bn » Tue Sep 03, 2024 4:24 pm

Hi all,

I have modified my version of coloring text in the "OriginalText.txt" file Kaveh provided.

This is just to make it functional. No need to use it... Just a demo how to use styledText to do the job.

In the first version I did not take into account that the .txt file is UTF-8 encoded.
Furthermore I removed all html code and line formatting in the description texts (that may not be what is wanted but it made it easier to code and Kaveh did not indicate a use case for colorisation.)

Kind regards
Bernd
Attachments
useStyledTextKaveh_2.livecode.zip
(1.75 KiB) Downloaded 38 times

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 529
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Regex for LiveCode

Post by kaveh1000 » Tue Sep 03, 2024 4:40 pm

Oh my goodness. Stam was right that I gave him credit for the app you kindly produced! I did not even look at the author. I assumed it was him. You are both my heroes now!!

I will give this a go later. Determined to understand styledText. :-)
Kaveh

stam
Posts: 2962
Joined: Sun Jun 04, 2006 9:39 pm

Re: Regex for LiveCode

Post by stam » Tue Sep 03, 2024 6:42 pm

bn wrote:
Tue Sep 03, 2024 4:24 pm
Just a demo how to use styledText to do the job.
A masterclass as always Bernd ;)

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4083
Joined: Sun Jan 07, 2007 9:12 pm

Re: Regex for LiveCode

Post by bn » Tue Sep 03, 2024 7:43 pm

Hi Kaveh and Stam,

Thank you for your kind words.

I think the easiest way to understand styledText is to make a small text in a field with, say, 4 lines of 3 words each. Then set the color of 1 word of a line to any color you want.
Now in a button you

Code: Select all

put the styledText of field "myField" into tArray
breakpoint
Inspect the array and see how styledText handles the, well, styles.
Kind regards
Bernd

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 529
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Regex for LiveCode

Post by kaveh1000 » Wed Sep 04, 2024 9:07 am

Thank you Bernd. Extremely informative. For a start, I had no idea about breakpoint!!

I have tried it and can see how it works. Perhaps this will help me with my irrational fear of arrays!

And a quick question - am I right that arrays are much faster than loops, say, in general?
Kaveh

stam
Posts: 2962
Joined: Sun Jun 04, 2006 9:39 pm

Re: Regex for LiveCode

Post by stam » Wed Sep 04, 2024 9:12 am

I think manipulating arrays is faster than manipulating strings.
No way to avoid looping!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4083
Joined: Sun Jan 07, 2007 9:12 pm

Re: Regex for LiveCode

Post by bn » Wed Sep 04, 2024 3:47 pm

kaveh1000 wrote:
Wed Sep 04, 2024 9:07 am
And a quick question - am I right that arrays are much faster than loops, say, in general?
As Stam pointed out that you will have to loop.
But there are different loops, some faster some slower.

In my experience it all depends on what you are trying to achieve.
If you have a list, like in your example xml, working with the list in a repeat loop:

Code: Select all

repeat for each line aLine in myList
In this form aLine contains the text of that line and you can act upon it
The same list in the form

Code: Select all

repeat with i = 1 to the number of lines of myList
   put line i of myList into tLineText
   ... 
is very slow for longer lists because on each iteration you force LC to fetch line i of myList to put it into a variable.

On the other hand creating an array from a list

Code: Select all

split myList by return
has its overhead too. Creating an array is not free.

Then it also depends on how you access the array.
In above sample you have an array that is numerically indexed by a key
However in arrays the keys are not sorted numerically. You would have to put the keys into a list and sort that list to use the keys from the list to access the array values.
Stam uses a faster approach:

Code: Select all

repeat for each element anElement in tArray
But you have to know whether your particular array is suited for this approach.


I attach a stack that demonstrates different approaches to loop for lists. Please have a look at the code.
Click on a radio button and then try different versions of loops.
It uses the colorNames of LC to create lists of different lengths to compare speed.

Kind regards
Bernd
Attachments
variousWaysToOperateOnListsKaveh.livecode.zip
(2.02 KiB) Downloaded 210 times

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”