matchtext and replacetext regex parts not working as expecte

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Ed_Ray
Posts: 83
Joined: Sun Jun 19, 2016 12:18 am

matchtext and replacetext regex parts not working as expecte

Post by Ed_Ray » Sat Jul 02, 2016 10:54 am

============================================================
matchtext, return multiple matches?

Postby trenatos » Wed Dec 17, 2014 1:21 pm
I'm having trouble figuring out how to return multiple matches from matchtext.

I'm also wondering why the engine blows up on (?ig) ? (Setting regex to case insensitive and global, as per PCRE)

My regex works, built with https://www.regex101.com/ set to PCRE

=============================================================

I never saw any answer to this past post (ABOVE) and I am having the same issue without finding any forum entry that is related.

I am only getting one match using the matchtext function using the “foundTextVarsList” variables that match up with my regex captured numbered groups even though with ANY OTHER regex editor tool online or desktop, I get all three matches.

Question 1:
Is there any way to make the matchtext function work globally so I can get my three matches?

Question 2:
How to do a regex replace that lets me use my group numbers like \2 or $2 to selectively replace distinct matches with distinct info tailored to each specific match

ALL my other online or desktop regex replacement string tools seem based on my actual numbered groups like \2 or $2, related to my regex parenthesized groups.

When I use the actual group number in the replacement string such as “<img src=\2” or “<img src=$2” it actually replaces all my matches with the right image name and extension based on what regex group 2 had stipulated for EACH individual match.

In other words for each match the image and extension can be different as long it satisfies the group regex. All my other regex replacement tools don’t just replace the three matches with the SAME image name as is happening when I use the replaceText function and the replacement string uses one of the group names from the matchtext “foundTextVarsList” variables.

Not being able to do this, and having the three distinct image named matches replaced with the same name “kills” most of the power of what a regex replacement is supposed to do for one’s different matches. In other words, what a regex replacement is supposed to do is use the group expression to tailor the match replacement as long as the replacement string meets the requirements of the group regex.

Am I missing something here or am I supposed to be using other regex functions I am not aware of?
Meaning perhaps the process is more cumbersome than what I was expecting based on using different online and desktop regex match and replacement tools that ALL work as I had previously described.

Any guidance here would be very much appreciated. Thanks in advance to all.

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

Re: matchtext and replacetext regex parts not working as exp

Post by Thierry » Sat Jul 02, 2016 1:26 pm

Ed_Ray wrote:
Question 1:
Is there any way to make the matchtext function work globally so I can get my three matches?

Question 2:
How to do a regex replace that lets me use my group numbers like \2 or $2 to selectively replace distinct matches with distinct info tailored to each specific match
Hi Ed_Ray,

If you are interested, contact me off-list.

Kind regards,

Thierry
Last edited by Thierry on Thu Nov 17, 2022 12:29 pm, edited 1 time in total.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

mimu
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 76
Joined: Tue Mar 05, 2013 7:00 pm
Location: Berlin
Contact:

Re: matchtext and replacetext regex parts not working as e

Post by mimu » Sun Jul 03, 2016 1:37 pm

Hello Thierry,

this function looks perfect, what will be the price for it?

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: matchtext and replacetext regex parts not working as exp

Post by MaxV » Mon Jul 04, 2016 9:45 am

To receive multiple answers from matchtext, just ues the repeat loop, like this:
########CODE#######
put 0 into cont
repeat for each word tword in field 1
add 1 to cont
if matchtext(tWord,myRegulaExpression) then
set the textStyle of word cont of field 1 to "bold"
end if
end repeat
#####END OF CODE#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: matchtext and replacetext regex parts not working as exp

Post by rinzwind » Tue Jul 05, 2016 7:52 pm

Regex is severly limited in LiveCode and needs improving. Kind of sad for a language so text friendly. And if they cant do it themselfs they need to contact Thierry ;

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9838
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: matchtext and replacetext regex parts not working as exp

Post by FourthWorld » Tue Jul 05, 2016 10:47 pm

rinzwind, if you'll share the IDs if the report you filed for that I'll be happy to see if I can help triage it toward resolution.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: matchtext and replacetext regex parts not working as exp

Post by rinzwind » Fri Jul 08, 2016 5:10 pm

Its basic functioanlity that other languages have for many many years including the scripting onces like vbscript.
http://forums.livecode.com/viewtopic.ph ... 30#p143958
http://forums.livecode.com/viewtopic.php?f=6&t=24892

Anyone with some regex experience knows this (Im certainly no expert). The devs do too but somehow cant find a way to make it a first class feature. Now regex just cant show all its potential in LC...

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9838
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: matchtext and replacetext regex parts not working as exp

Post by FourthWorld » Fri Jul 08, 2016 6:28 pm

rinzwind wrote:Its basic functioanlity that other languages have for many many years including the scripting onces like vbscript.
http://forums.livecode.com/viewtopic.ph ... 30#p143958
http://forums.livecode.com/viewtopic.php?f=6&t=24892

Anyone with some regex experience knows this (Im certainly no expert). The devs do too but somehow cant find a way to make it a first class feature. Now regex just cant show all its potential in LC...
Yes, it can be useful to share thoughts with other users in this user-to-user forum, but unless someone posts those to the bug DB the odds of the team stumbling across those requests is low.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Ed_Ray
Posts: 83
Joined: Sun Jun 19, 2016 12:18 am

Re: matchtext and replacetext regex parts not working as exp

Post by Ed_Ray » Fri Jul 08, 2016 9:35 pm

I am the original poster of these issues. Sorry for the late rely back. For some strange reason I did not receive any email notifying me of any responses to my post so I was simply unaware of your replies.

Thanks to all you folks that responded. I have become so frustrated with the regex functions matchtext, matchchunk, replacetext (which expect you to construct and make your own global answer) and the filter which when you ask to filter for unmatched results does not even list them, I decided to make a function that utilizes some of these incomplete and unsatisfactory build in functions, in order to supply all the right answers in one gulp for all the users out there to benefit from.

I am still not finished but as soon as I complete it I will put it out here to see what you guys think of it and get you feedback.

Meanwhile I will occasionally check back on any further responses to this original post as I attempt to complete the complete regex solution I had previously stated.

Thanks

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 474
Joined: Thu Sep 04, 2008 6:23 am
Location: Melbourne Australia

Re: matchtext and replacetext regex parts not working as exp

Post by jameshale » Mon Jul 11, 2016 10:37 am

It would be good to know which version of LC you are using.

I use MatchChunk extensively with more than one search expression (search substrings) and do not have a problem.

Sometimes in a structure like this:

Code: Select all

  repeat while MatchChunk(container,regexexp,s1,e1,s2,e2) 
    do stuff
  end repeat
In the above "regexexp" is the regex with, in this case two substrings as in...

Code: Select all

  put "(<a .*?)(href=" & quote & ".*?" & quote & ")" into regexexp
This form was used to look for multiple instances of the desired string structures in a document (identifying internal links within a epub)

Another example from looking into epubs, here is a function to extract a particular string formation (the second substring identifying the cover file) only if another substring (the first) is present too.

Code: Select all

function get_cover_link pcoverpage
   put empty into s1
   put empty into e1
   put empty into s2
   put empty into e2
   put "(?i)(src="&quote&")([^"&quote&"]*?"&quote&")" into mtext
   if MatchChunk(pcoverpage,mtext,s1,e1,s2,e2) then
      put char s2 to e2 of pcoverpage into thelink
      if char 1-3 of thelink = "../" then put char 4 to -1 of thelink into thelink
   end if
   return thelink
end get_cover_link
Thierry helped me understand these regex forms and also the need to construct your regular expressions, place them in a variable and use that construction in the MatchChunk (et al) function.

There was an error in the MatchChunk in a particular case (http://quality.livecode.com/show_bug.cgi?id=17245) but this was fixed at the beginning of the 8 cycle.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: matchtext and replacetext regex parts not working as exp

Post by richmond62 » Mon Jul 11, 2016 6:13 pm

Frankly matchText and replaceText leave me completely cold.

I tend to just search for a word and then use replace:

on mouseUp
put 1 into WWORD
repeat until word WWORD of fld "WTP" is empty
if word WWORD of fld "WTP" = "Pooh" then
replace "Pooh" with "XYZ" in fld "WTP"
end if
add 1 to WWORD
end repeat
end mouseUp
Attachments
text games.livecode.zip
Here's the stack
(2.07 KiB) Downloaded 206 times

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9838
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: matchtext and replacetext regex parts not working as exp

Post by FourthWorld » Mon Jul 11, 2016 6:15 pm

jameshale wrote:There was an error in the MatchChunk in a particular case (http://quality.livecode.com/show_bug.cgi?id=17245) but this was fixed at the beginning of the 8 cycle.
Thanks for that update, James.

If you - or anyone else here - finds an issue with LiveCode's regex implementation please submit a bug report on it so the team can address it as they did with that one.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jameshale
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 474
Joined: Thu Sep 04, 2008 6:23 am
Location: Melbourne Australia

Re: matchtext and replacetext regex parts not working as exp

Post by jameshale » Tue Jul 12, 2016 1:24 am

FourthWorld wrote: Thanks for that update, James.

If you - or anyone else here - finds an issue with LiveCode's regex implementation please submit a bug report on it so the team can address it as they did with that one.
Actually Thierry reported the bug and then Thierry fixed it. Most grateful.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9838
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: matchtext and replacetext regex parts not working as exp

Post by FourthWorld » Tue Jul 12, 2016 1:50 am

Good to know the open source process is working. Thierry's pull request wasn't linked there; it would be interesting to see whether the fix was an update to the regex lib or something deeper.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

charms
Posts: 122
Joined: Mon Feb 10, 2014 6:21 pm
Location: Singapore
Contact:

Re: matchtext and replacetext regex parts not working as expecte

Post by charms » Sat Mar 10, 2018 1:09 pm

Opened bug on this: 21054.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”