Page 1 of 1
regex look-ahead, look-behind?
Posted: Wed Jan 28, 2015 11:10 am
by kaveh1000
Am I right that these are not implemented in the regex engine in LiveCode? I am using this code:
to find the word "TeX" when not preceded or followed by another letter.
Re: regex look-ahead, look-behind?
Posted: Wed Jan 28, 2015 12:39 pm
by Thierry
kaveh1000 wrote:Am I right that these are not implemented in the regex engine in LiveCode?
Umm, since LC 6.5, PCRE lib is 8.33, before it was 6.7.
From memory, since PCRE 7.?, lookaround have been implemented.
That said, I never had any problems with lookaround so far.
from what I see, it should work; could be a typo, some extra chars, or not enough coffee?
I am using this code:
to find the word "TeX" when not preceded or followed by another letter.
I give you a secret here:
We rarely need lookarounds. You can write your regex in another way.
And not the least, lookbehind are not efficient at all.
Could you try this one, for instance:
Regards,
Thierry
Re: regex look-ahead, look-behind?
Posted: Wed Jan 28, 2015 12:48 pm
by kaveh1000
Thanks a lot Thierry. I did eventually find that \b and it works
But there must be instances where look-ahead etc are the only way, or do you think not? If not, I would be happier not to use them as the code is simpler too and as you say faster.
thanks for the advice.
Regards
Kaveh
Re: regex look-ahead, look-behind?
Posted: Wed Jan 28, 2015 1:04 pm
by Thierry
kaveh1000 wrote:Thanks a lot Thierry. I did eventually find that \b and it works
But there must be instances where look-ahead etc are the only way, or do you think not? If not, I would be happier not to use them as the code is simpler too and as you say faster.
thanks for the advice.
Regards
Kaveh
lookbehind is not efficient.
And, again from memory, Javascript and Ruby < 1.9 didn't have any lookbehind implementation
and I don't think they have any problems with writing their regex...
That said, I don't like too much to generalize as exceptions are part of the game, but
feel free to send me a regex where you think it's not possible to not to use lookarounds,
and I'll be happy to try to code it without whenever I have a bit of free time
Regards,
Thierry
Re: regex look-ahead, look-behind?
Posted: Thu Jan 29, 2015 1:12 am
by kaveh1000
OK Thierry. There is no escape now! How do I replace a pattern in LiveCode? So simple case of transposing two words, change
(abc)(def) to
\2\1
I tried \1 but I get a literal \1. Thanks.
Re: regex look-ahead, look-behind?
Posted: Thu Jan 29, 2015 8:43 am
by Thierry
kaveh1000 wrote:OK Thierry. There is no escape now!
Hi Kaveh,
You mean no escape for me ?
How do I replace a pattern in LiveCode?
So simple case of transposing two words, change
(abc)(def) to
\2\1
I tried \1 but I get a literal \1.
The common mistake we do is to think that replacteText(), matchXXX() and the replacementString
are part of the regex; This is just wrong thinking.
PCRE doesn't know anything about these functions and especially the replacementString.
Back references being part of the regex,
you can have them *only* in the second parameter of the 3 functions mentionned above.
Here is an un-official solution to, as you said, this simple case of transposing 2 words:
Code: Select all
put "Thierry Douez LiveCode Regex" into theText
put "(\w+)\s+(\w+)" into theRegex
put "\2 \1" into smartReplacement
if sunnYreplace( theText, theRegex, smartReplacement, outString) then
answer theText & " is now: " & outString
end if
and to see it in action:
Now, the classic way is to use matchChunk() and to play with the resulting indexes to swap the words;
but it is more work for the coder, prone to more errors and less easy to read the script
Regards,
Thierry
Re: regex look-ahead, look-behind?
Posted: Thu Jan 29, 2015 8:14 pm
by kaveh1000
Yes, I meant no escape for you Thierry...
As time was short I have had to write my script another way (BBEdit and AppleScript), but I will be back to try your suggestion and to think about what you said about replacement etc. I remember a year or so ago you came to my aid (converting tab delimited text with regex), so I know you are the regex guy and Mark too of course.
Back soon!
Re: regex look-ahead, look-behind?
Posted: Fri Jan 30, 2015 2:29 am
by Thierry
kaveh1000 wrote:
As time was short I have had to write my script another way (BBEdit and AppleScript),
BBEdit is a perfect tool for that and
you certainly know that "Bare Bones" uses the PCRE library
So, keep your regexes,
as they will be almost fully compatible with livecode ones.
Good luck for your project,
Thierry
Re: regex look-ahead, look-behind?
Posted: Fri Jan 30, 2015 10:43 am
by kaveh1000
Thierry wrote:kaveh1000 wrote:
As time was short I have had to write my script another way (BBEdit and AppleScript),
BBEdit is a perfect tool for that and
you certainly know that "Bare Bones" uses the PCRE library

Thierry
I told you there was no escape! Going off topic here but the only way I found of stringing together several regexp replacements as a script is by recording each and saving as an AppleScript. Unfortunately AppleScript is so bloated, and also escapes characters again, leading to unreadable code, e.g.
Code: Select all
replace "{\\[}{\\[}(.+){\\]}{\\]}" using "~\\\\ref{\\1}" searching in text 1 of text document "rfp.tex" options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
Any quick hint of another way of doing this within BBEdit?
Re: regex look-ahead, look-behind?
Posted: Fri Jan 30, 2015 11:37 am
by Thierry
kaveh1000 wrote:
<...>
the only way I found of stringing together several regexp replacements as a script is by recording each and saving as an AppleScript. Unfortunately AppleScript is so bloated, and also escapes characters again, leading to unreadable code..
Any quick hint of another way of doing this within BBEdit?
As it goes off-topic,
please feel free to contact me off-list.
Regards,
Thierry