replaceText regex bit useless

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
rinzwind
Posts: 131
Joined: Tue May 01, 2012 10:44 am

replaceText regex bit useless

Post by rinzwind » Thu Jul 23, 2015 10:57 am

put replaceText(text, "(?-x)([()*+,/=-])", " $1 ") into text
This should add spaces before and after the mentioned characters... but instead it just replaces it with the literal text " $1 "... VBScript can do this.
It won't work. The regex is PCRE2 10.10 complaint.
Why doesnt livecode support back references if it uses PCRE libraries to handle things (or at least states it PCRE compatible)?
Also matching groups (calling a method for each match) would be useful.
Wlso why doesnt matchchunk returns an array or item delimited string as last parameter holding all matches? Would be way more useful and efficient than the way its now.

regex support is rather limited it seems (and just now I'm getting used to regex for solving some text processing tasks)

any alternative? LiveCode text handling is great, but only if it follows the rules that you want to do... if the word/token/char spec does not do what you want your stuck because of inflexibility.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 648
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: replaceText regex bit useless

Post by Thierry » Thu Jul 23, 2015 11:25 am

Hi rinzwind,

You might find interesting this post:

http://forums.livecode.com/viewtopic.ph ... 09#p128076

or this url:

http://sunny-tdz.com/sunnyrex

Regards,

Thierry
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2826
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: replaceText regex bit useless

Post by mwieder » Thu Jul 23, 2015 6:55 pm

rinzwind -

it's because there an error in your code. What you want is

Code: Select all

put replacetext(text, "(?-x)([()*+,/=-])", " " & $1 & " ") into text
that said, the LiveCode engine currently is using the PCRE 8.33 library.

rinzwind
Posts: 131
Joined: Tue May 01, 2012 10:44 am

Re: replaceText regex bit useless

Post by rinzwind » Thu Jul 23, 2015 9:19 pm

Nope... did you try it? It replaces the matches with spaces. Just ignores $1.

a=1 should result in a = 1

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2826
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: replaceText regex bit useless

Post by mwieder » Thu Jul 23, 2015 11:34 pm

Ah, ok - I see what you're trying to do now.
It won't work that way because the replacementString for the replacetext command isn't a regex string.
You'll have to get a bit more complicated to accomplish that goal. How's this?:

Code: Select all

   if matchtext(tText, "([()*+,/=-])", tVar) then
      put replacetext(tText, "([()*+,/=-])", space & tVar & space) into tText
   end if
(it is, by the way, a not-so-good idea to use a keyword ('text') as a variable name. The compiler is likely to get confused.)

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 648
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: replaceText regex bit useless

Post by Thierry » Fri Jul 24, 2015 6:46 am

How's this?:

Code: Select all

   if matchtext(tText, "([()*+,/=-])", tVar) then
      put replacetext(tText, "([()*+,/=-])", space & tVar & space) into tText
   end if
Mmm Mark, not too good :roll:

I've simplified the regex but kept your logic and see what happen then:

Code: Select all

on mouseUp
   put "1  2  3  4   5" into tText
    if matchtext(tText, "(\d)", tVar) then
            put replacetext(tText, "(\d)",  tVar & tVar) into tText
      end if
   put Ttext --> 11 11  11  11   11
end mouseUp
That's because of this kind of questionning I did write sunnYrex; i.e:

Code: Select all

if sunnYreplace( tText, "(\d)", "\1\1", tResult) then
   put tResult -> 11 22 33 44 55
end if
Best,

Thierry
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2826
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: replaceText regex bit useless

Post by mwieder » Fri Jul 24, 2015 6:14 pm

Well, yes, but with the limited example given of "a=1" it performs as expected.
If you need to process the entire line then you'd need regex looping, and the nfa processing is too much for my head this morning. :P

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 648
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

Re: replaceText regex bit useless

Post by Thierry » Fri Jul 24, 2015 6:18 pm

mwieder wrote:Well, yes, but with the limited example given of "a=1" it performs as expected.
If you need to process the entire line then you'd need regex looping, and the nfa processing is too much for my head this morning. :P
Yep, better take 2 cups of coffee before :P

Best,

Thierry
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

rinzwind
Posts: 131
Joined: Tue May 01, 2012 10:44 am

Re: replaceText regex bit useless

Post by rinzwind » Tue Jul 28, 2015 7:38 pm

Well.. if they use v 8 of PCRE that would explain it. That one does not support find and replace with regex replacement according to regexmagic ;). PCRE2 v 10 does. Conveniently release in 2015. Long due I guess.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2826
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: replaceText regex bit useless

Post by mwieder » Wed Jul 29, 2015 12:07 am

Well, that and the fact that the LiveCode replacetext command doesn't take regex syntax for its replacement value string, thus the two-step approach.

Also PCRE2 v10 has some api changes that are going to take some time to grok before implementing. It's not just a straight drop-in replacement.

Post Reply

Return to “Feature Requests”