Page 1 of 1

replaceText regex bit useless

Posted: Thu Jul 23, 2015 10:57 am
by rinzwind
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.

Re: replaceText regex bit useless

Posted: Thu Jul 23, 2015 11:25 am
by Thierry
Hi rinzwind,

You might find interesting this post:

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

Regards,

Thierry

Re: replaceText regex bit useless

Posted: Thu Jul 23, 2015 6:55 pm
by mwieder
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.

Re: replaceText regex bit useless

Posted: Thu Jul 23, 2015 9:19 pm
by rinzwind
Nope... did you try it? It replaces the matches with spaces. Just ignores $1.

a=1 should result in a = 1

Re: replaceText regex bit useless

Posted: Thu Jul 23, 2015 11:34 pm
by mwieder
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.)

Re: replaceText regex bit useless

Posted: Fri Jul 24, 2015 6:46 am
by Thierry
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

Re: replaceText regex bit useless

Posted: Fri Jul 24, 2015 6:14 pm
by mwieder
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

Re: replaceText regex bit useless

Posted: Fri Jul 24, 2015 6:18 pm
by Thierry
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

Re: replaceText regex bit useless

Posted: Tue Jul 28, 2015 7:38 pm
by rinzwind
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.

Re: replaceText regex bit useless

Posted: Wed Jul 29, 2015 12:07 am
by mwieder
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.