simple regex replacetext question

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
slindstrom
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 29
Joined: Mon Apr 09, 2012 8:53 pm

simple regex replacetext question

Post by slindstrom » Wed Mar 06, 2019 12:27 am

At least I think it's simple, though I've not figured out the syntax. How should I modify the following so that it works even if the contents of the table tag has a line break and sequence of spaces at the start of the second line? I've tried searching this forum but inevitably find discussions that are more complicated. Regex mostly confounds me, I find myself easily led astray. Thanks in advance!

put replaceText(tVar,"<table.*?>","<table>") into tVar

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: simple regex replacetext question

Post by bogs » Wed Mar 06, 2019 11:59 am

While I replace very little text, I think when it comes to replacing html elements, you have to break them up (something to do with Lc parsing html).

For instance, i wrote a little utility to create a blockquote a long time ago. It wouldn't work till I broke the code to create the blockquote itself up -

Code: Select all

 put "<blockquote style=" & quote & "background-color: silver;" & quote & ">" & return & "<br>" before field 1
I also seem to remember this question coming up before a while back. If I come across that thread again, I'll post a link here.

*Edit - it might have been this thread, towards the bottom there are a couple of solutions for html and regex.
Image

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

Re: simple regex replacetext question

Post by Thierry » Wed Mar 06, 2019 12:35 pm

slindstrom wrote: put replaceText(tVar,"<table.*?>","<table>") into tVar
Hi,

Please try this one:

Code: Select all

replaceText( tVar,"(?ms)<table[^>]*?>","<table>")
or:

Code: Select all

replaceText( tVar,"(?ms)(?<=<table)[^>]*?(?=>)","")
Disclaimer: not tested, and as it is true ALL the time, writing a valid regex depends on the data.
So take this with a grain of salt, and test it with *your* datas.

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: simple regex replacetext question

Post by bogs » Wed Mar 06, 2019 12:55 pm

OOOoooo, that is slick Thierry. BTW, I love that function you wrote from the linked thread, it has come in handy many times since I found it, I give it 4 thumbs up ImageImageImageImage (needed my last thumb to hit enter) :D
Image

slindstrom
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 29
Joined: Mon Apr 09, 2012 8:53 pm

Re: simple regex replacetext question

Post by slindstrom » Wed Mar 06, 2019 6:22 pm

Thanks, Thierry! Both work with my data.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: simple regex replacetext question

Post by SparkOut » Wed Mar 06, 2019 6:45 pm

slindstrom wrote:
Wed Mar 06, 2019 6:22 pm
Thanks, Thierry! Both work with my data.
That's what I'd expect from Thierry, he's the regex hero.


---


Hi Thierry!

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: simple regex replacetext question

Post by Klaus » Wed Mar 06, 2019 6:50 pm

He's the Regex 'ero! :D

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

Re: simple regex replacetext question

Post by Thierry » Wed Mar 06, 2019 8:10 pm

slindstrom wrote: Thanks, Thierry! Both work with my data.
I'm glad that it works for you.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Post Reply

Return to “Talking LiveCode”