replaceText regex bit useless
Moderator: Klaus
replaceText regex bit useless
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.
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
Hi rinzwind,
You might find interesting this post:
http://forums.livecode.com/viewtopic.ph ... 09#p128076
Regards,
Thierry
You might find interesting this post:
http://forums.livecode.com/viewtopic.ph ... 09#p128076
Regards,
Thierry
Last edited by Thierry on Thu Nov 17, 2022 12:33 pm, edited 1 time in total.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: replaceText regex bit useless
rinzwind -
it's because there an error in your code. What you want is
that said, the LiveCode engine currently is using the PCRE 8.33 library.
it's because there an error in your code. What you want is
Code: Select all
put replacetext(text, "(?-x)([()*+,/=-])", " " & $1 & " ") into text
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: replaceText regex bit useless
Nope... did you try it? It replaces the matches with spaces. Just ignores $1.
a=1 should result in a = 1
a=1 should result in a = 1
Re: replaceText regex bit useless
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?:
(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.)
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: replaceText regex bit useless
Mmm Mark, not too goodHow's this?:
Code: Select all
if matchtext(tText, "([()*+,/=-])", tVar) then put replacetext(tText, "([()*+,/=-])", space & tVar & space) into tText end if

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
Code: Select all
if sunnYreplace( tText, "(\d)", "\1\1", tResult) then
put tResult -> 11 22 33 44 55
end if
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: replaceText regex bit useless
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.
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.

PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: replaceText regex bit useless
Yep, better take 2 cups of coffee beforemwieder 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.

Best,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: replaceText regex bit useless
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
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.
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.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev