Difficult find and replace problem (for me)

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

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

Re: Difficult find and replace problem (for me)

Post by richmond62 » Wed Jun 24, 2020 6:45 pm

Unicode® Technical Standard #18
Unicode Regular Expressions

https://www.unicode.org/reports/tr18/tr18-21.html

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Difficult find and replace problem (for me)

Post by marksmithhfx » Wed Jun 24, 2020 8:49 pm

richmond62 wrote:
Tue Jun 16, 2020 3:42 pm
I couldn't understand any of your code, so I wrote my own. :?
Hi Richmond, the basic premise was I wanted to take a string like this: FirstPart[19]SecondPart and turn it into just FirstPartSecondPart, dropping the bit in brackets. When I want to do something like that I generally don't concern myself with whether the [text] is in the beginning, middle or end and just write code to handle it anywhere. So...

Code: Select all

      put offset ("[", tline) into tStart -- see if we have a [
      if tStart > 0 then -- yup, so find the closing match
         put offset ("]", tline) into tEnd -- got it
         if tEnd > tStart then -- if the end is greater than the start ie. [ ]
            put char 1 to tStart-1 of tline into tFirst -- put the preceeding bit into First
            put char tEnd + 1 to the number of chars of tline of tline into tLast -- put the succeeding bit into Last
            put tFirst & tLast into tline -- put First and Last together, all done
         end if
      end if
But as it turns out, this 1 line of LCscript including regex expression will do just as well:

Code: Select all

put replacetext(tline, "(\[[^\]]+])", empty) into tline
Last edited by marksmithhfx on Wed Jun 24, 2020 8:54 pm, edited 1 time in total.
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

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

Re: Difficult find and replace problem (for me)

Post by richmond62 » Wed Jun 24, 2020 8:51 pm

richmond62 wrote: ↑
Tue Jun 16, 2020 4:42 pm
I couldn't understand any of your code, so I wrote my own. :?
Err . . . actually . . . I lied. 8)

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Difficult find and replace problem (for me)

Post by marksmithhfx » Sat Jun 27, 2020 1:30 pm

richmond62 wrote:
Wed Jun 24, 2020 8:51 pm
richmond62 wrote: ↑
Tue Jun 16, 2020 4:42 pm
I couldn't understand any of your code, so I wrote my own. :?
Err . . . actually . . . I lied. 8)
You're forgiven 8)

Did you see my post edit added after you replied? I added the regex example to have them both in the same place. Also Thierry responded somewhere back a ways in this thread. I'll have to go study that.. it's greek stuff (or maybe geek stuff) :lol:
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Difficult find and replace problem (for me)

Post by marksmithhfx » Sat Jun 27, 2020 1:42 pm

Thierry wrote:
Wed Jun 24, 2020 5:46 pm
2. find the literal "[" followed by any number of characters but NOT "]" and lastly "]"
the escaped \[ is mandatory because [ is a meta-character for regex.
the carret inside [ ] means NOT.
Thanks, helps (me) if I am working through a specific problem or solution, so this is great. Also, many thanks for the link to the RegexBuddy page. It looks like a great intro.

Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

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

Re: Difficult find and replace problem (for me)

Post by Thierry » Sat Jun 27, 2020 3:10 pm

marksmithhfx wrote:
Sat Jun 27, 2020 1:42 pm
Thanks, helps (me) if I am working through a specific problem or solution, so this is great.
Also, many thanks for the link to the RegexBuddy page. It looks like a great intro.
You're welcome.
Actually, that's my primary documentation when in a need for some.
I'll have to go study that.. it's greek stuff (or maybe geek stuff)
Someone said that regex has a vertical learning curve.
I'm not so sure, it's so vertical, but yes, it certainly takes time to get into it.
Of course, I'm not talking of copy-paste a regex from the Net
and then crying in a forum for help :)

If you accept an analogy, one can't play Chess until he knows the rules,
and knowing the rules is not enough to be a good Chess player.

And last, as I did promess you,
here is one way to parse your Capital.txt entry.

for Mark.jpg

And this one would be my way :roll:

for Mark 2.jpg

Best wishes,

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

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Difficult find and replace problem (for me)

Post by marksmithhfx » Mon Jun 29, 2020 12:17 pm

Thierry wrote:
Sat Jun 27, 2020 3:10 pm
And last, as I did promess you,
here is one way to parse your Capital.txt entry.


for Mark.jpg


And this one would be my way :roll:


for Mark 2.jpg


Best wishes,

Thierry
Wonderful, thank you so much for providing those. I will have to take some time to deconstruct what you are doing and compare it to my own. Interesting how many different ways there are to accomplish the same thing in programming.

Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”