Page 1 of 1

Fast insertion of text in field using Regex

Posted: Tue Apr 23, 2019 6:23 pm
by kaveh1000
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.

Re: Fast insertion of text in field using Regex

Posted: Tue Apr 23, 2019 7:06 pm
by bogs
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.

Re: Fast insertion of text in field using Regex

Posted: Tue Apr 23, 2019 7:35 pm
by FourthWorld
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?

Re: Fast insertion of text in field using Regex

Posted: Tue Apr 23, 2019 7:36 pm
by kaveh1000
Agree. Good point. I learnt that a long time ago (possibly from yourself!)

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

Re: Fast insertion of text in field using Regex

Posted: Tue Apr 23, 2019 7:36 pm
by dunbarx
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

Re: Fast insertion of text in field using Regex

Posted: Tue Apr 23, 2019 7:40 pm
by kaveh1000
@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.

Re: Fast insertion of text in field using Regex

Posted: Tue Apr 23, 2019 9:44 pm
by bogs
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