regex look-ahead, look-behind?

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: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

regex look-ahead, look-behind?

Post by kaveh1000 » Wed Jan 28, 2015 11:10 am

Am I right that these are not implemented in the regex engine in LiveCode? I am using this code:

Code: Select all

(?<![A-Za-z])TeX(?![A-Za-z])
to find the word "TeX" when not preceded or followed by another letter.
Kaveh

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: regex look-ahead, look-behind?

Post by Thierry » Wed Jan 28, 2015 12:39 pm

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:

Code: Select all

(?<![A-Za-z])TeX(?![A-Za-z])
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:

Code: Select all

(\bTeX\b)
Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: regex look-ahead, look-behind?

Post by kaveh1000 » Wed Jan 28, 2015 12:48 pm

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
Kaveh

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: regex look-ahead, look-behind?

Post by Thierry » Wed Jan 28, 2015 1:04 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: regex look-ahead, look-behind?

Post by kaveh1000 » Thu Jan 29, 2015 1:12 am

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.
Kaveh

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: regex look-ahead, look-behind?

Post by Thierry » Thu Jan 29, 2015 8:43 am

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:
sunnYrex screen3.png

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: regex look-ahead, look-behind?

Post by kaveh1000 » Thu Jan 29, 2015 8:14 pm

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!
Kaveh

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: regex look-ahead, look-behind?

Post by Thierry » Fri Jan 30, 2015 2:29 am

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: regex look-ahead, look-behind?

Post by kaveh1000 » Fri Jan 30, 2015 10:43 am

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?
Kaveh

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: regex look-ahead, look-behind?

Post by Thierry » Fri Jan 30, 2015 11:37 am

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply