matchtext and replacetext regex parts not working as expecte
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
matchtext and replacetext regex parts not working as expecte
============================================================
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.
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.
Re: matchtext and replacetext regex parts not working as exp
Hi Ed_Ray,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
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.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
-
- 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
Hello Thierry,
this function looks perfect, what will be the price for it?
this function looks perfect, what will be the price for it?
Re: matchtext and replacetext regex parts not working as exp
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#####
########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
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: matchtext and replacetext regex parts not working as exp
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 ;
-
- 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
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: matchtext and replacetext regex parts not working as exp
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...
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...
-
- 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
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.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...
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: matchtext and replacetext regex parts not working as exp
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
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
-
- 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
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:
In the above "regexexp" is the regex with, in this case two substrings as in...
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.
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.
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
Code: Select all
put "(<a .*?)(href=" & quote & ".*?" & quote & ")" into regexexp
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=""e&")([^""e&"]*?""e&")" 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
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.
-
- 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
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
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
-
- 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
Thanks for that update, James.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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- 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
Actually Thierry reported the bug and then Thierry fixed it. Most grateful.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.
-
- 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
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: matchtext and replacetext regex parts not working as expecte
Opened bug on this: 21054.