Fast insertion of text in field using Regex

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 508
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Fast insertion of text in field using Regex

Post by kaveh1000 » Tue Apr 23, 2019 6:23 pm

Best if I give a simple example.

Suppose I have the following text in a field:

Code: Select all

abcd
bcds
zaaad
I want to put • before each latter that comes before a "d". So this should change to

Code: Select all

b•cds
bb•cd
zaa•ad
(pls assume the pattern is much more complex so we need to use regex)

If we had full regex with backreferencing, I would change

.d to •&

But in LiveCode we cannot use backreferencing, so I need to use a loop to find every instance with matchchunk, then do a replacetext.

My aim is to achieve the above substitution in the fastest way possible in LiveCode

I thought I could use lookahead, so that the search expression is non-selecting. so replace

(?=.d) with •

But it seems non-selected text cannot be replaced.

Any comments appreciated.
Kaveh

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Fast insertion of text in field using Regex

Post by bogs » Tue Apr 23, 2019 7:06 pm

The first thing I'd suggest is putting that field into a variable, and only then doing whatever it is you have to do. Even the fastest fields I've worked with (I believe 6.x) were orders of magnitude slower than the clunkiest repeat on a variable.
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Fast insertion of text in field using Regex

Post by FourthWorld » Tue Apr 23, 2019 7:35 pm

kaveh1000 wrote:
Tue Apr 23, 2019 6:23 pm
My aim is to achieve the above substitution in the fastest way possible in LiveCode
How many lines in the data?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Fast insertion of text in field using Regex

Post by kaveh1000 » Tue Apr 23, 2019 7:36 pm

Agree. Good point. I learnt that a long time ago (possibly from yourself!)

But I want to make it faster still even in variable..
Kaveh

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9655
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Fast insertion of text in field using Regex

Post by dunbarx » Tue Apr 23, 2019 7:36 pm

The first thing I would do is look for a non-regex solution. But that is because regex makes me dizzy.

If you have your data in a fld 1, then in a button script:

Code: Select all

on mouseUp
   get fld 1
   repeat for each line tLine in it
      put "•" before char offset("d",tLine) - 1 in tLine
      put tLine & return after accum
   end repeat
  answer accum
end mouseUp
Now this may be slower that regEx if you have a bit more data to process than those three lines. This also will put the "•" at the beginning of the line if the "D" is too far in front.

Craig Newman

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

Re: Fast insertion of text in field using Regex

Post by kaveh1000 » Tue Apr 23, 2019 7:40 pm

@Richard:

I have given a simple example. Actually, there are many paragraphs and each para could have multiple cases of the text. There could be 1000s of matches.

@Craig:

I understand your headache with regex, but unfortunately the patterns are complex, and regex is the only solution. My example is trivial, in order to explain what I have to do. I always do try and use "replace" and "offset" when possible.
Kaveh

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Fast insertion of text in field using Regex

Post by bogs » Tue Apr 23, 2019 9:44 pm

kaveh1000 wrote:
Tue Apr 23, 2019 7:36 pm
I learnt that a long time ago (possibly from yourself!)
No, you would have gotten it from someone a lot smarter than myself, I am a slacker and use fields for all kinds of stuff. Of course, I'm not doing huge operations in them either :P
Image

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”