ReplaceText problem

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

Post Reply
Sjatplat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 75
Joined: Wed Jun 22, 2011 1:53 pm

ReplaceText problem

Post by Sjatplat » Fri Mar 21, 2014 4:38 pm

Hi - since the "word" chunk includes punctuation I want to find another way to replace only the word and not the punctation,

I'm trying the replaceText function but it gives me a strange output.

Code: Select all

put replaceText("[Apple].","[Apple]","Orange")
outputs:

[OrangeOrangeOrangeOrangeOrange].

what is happening here?

Sjat

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: ReplaceText problem

Post by Klaus » Fri Mar 21, 2014 4:49 pm

Hei Sjat,

no idea, but this is surely not what you exspected!? :D

What about this:

Code: Select all

on mouseUp
   put "Apple. Apple, Apple!" into tText
   put replaceText(tText,"Apple","Orange")
end mouseUp
-> Orange. Orange, Orange!


Best

Klaus

Sjatplat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 75
Joined: Wed Jun 22, 2011 1:53 pm

Re: ReplaceText problem

Post by Sjatplat » Fri Mar 21, 2014 5:03 pm

Thanks Klaus, I'm using variables.

There is something happening when the text contains square brackets....
I use square brackets in my text as references to be replaced with other words etc.

I found that using the chunk expression "word" it spits back the word AND period/punctuation mark. I need to use "word" because I don't want to replace ALL the words.
like this:

Code: Select all

on mouseup
put "[tomato]" into tMatchword
put field "text" into tText

repeat with tLoop=1 to the number of words in tText
if word tLoop of tText = tMatchword then put random(1000) into word tLoop of tText
end repeat
end mouseup
The problem with this is that the tMatchword will not be found if there is a punctuation mark after a word.

Sjat

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

Re: ReplaceText problem

Post by Thierry » Fri Mar 21, 2014 5:20 pm

Sjatplat wrote:I'm trying the replaceText function but it gives me a strange output.

Code: Select all

put replaceText("[Apple].","[Apple]","Orange")
outputs: [OrangeOrangeOrangeOrangeOrange].

what is happening here?
Umm, this is right :roll:

your regex contains a character class, which means any character inside the [ ]
will try to match some char in your text.

So, the A matches the A and is replaced by Orange,
then the p matches the p and is replaced by .., and so on.

If I understand what you want, try this one:

Code: Select all

put replaceText("[Apple].","\[Apple]","Orange")
Regards,

Thierry
Last edited by Thierry on Fri Mar 21, 2014 8:09 pm, edited 1 time in total.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: ReplaceText problem

Post by Klaus » Fri Mar 21, 2014 5:21 pm

Hej Sjat,

ah, I understand.

Sorry, but my example is the only bit of regex I understood until today :D
There are some Regex pros here in the forum and they will hopefully chime in.


Best

Klaus

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: ReplaceText problem

Post by Klaus » Fri Mar 21, 2014 5:22 pm

The Regex pro was a tad faster than me :D

Sjatplat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 75
Joined: Wed Jun 22, 2011 1:53 pm

Re: ReplaceText problem

Post by Sjatplat » Fri Mar 21, 2014 5:29 pm

Klaus, thanks anyway :D

Thierry, Perfect! That worked.

What does the "\" do?

Sjat

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

Re: ReplaceText problem

Post by Thierry » Fri Mar 21, 2014 6:53 pm

Sjatplat wrote:Thierry, Perfect! That worked.

What does the "\" do?
the square brackets have some meanings in a regex;
it's called a character class. If you want to match a square bracket then
you need to escape it, so it is seen as a single char. The trick is you need to
escape only the opening bracket;
the regex engine is smart and then understand what you want :)

Dos this make sense?

Regards,

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

Sjatplat
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 75
Joined: Wed Jun 22, 2011 1:53 pm

Re: ReplaceText problem

Post by Sjatplat » Fri Mar 21, 2014 10:01 pm

@Thierry

Yes, that makes sense, thank you. But now I want to know more about the regex engine :) Have any links?

BTW: do you think I should use curly brackets for my reference tags? just to make sure to be safe from future problems, I mean.

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: ReplaceText problem

Post by atout66 » Sun Mar 23, 2014 12:26 pm

Hi to all.
Followinf this regex post, why (Thierry :wink: ) the code following doesn't work despite the "\" char in front of the char "°" ?

Code: Select all

put "C'est la ligne n\°:" into laLigne
laLigne return the string "C'est la ligne n\:"

What am I missing ?
Discovering LiveCode Community 6.5.2.

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

Re: ReplaceText problem

Post by Thierry » Sun Mar 23, 2014 12:58 pm

atout66 wrote:Followinf this regex post, why (Thierry :wink: )
Désolé Monsieur,
I'm an expert only on cheese and some regex :)
atout66 wrote:why the code following doesn't work despite the "\" char in front of the char "°" ?

Code: Select all

put "C'est la ligne n\°:" into laLigne
laLigne return the string "C'est la ligne n\:"
What am I missing ?
Umm, should ask the support :roll:

If you want a quote in your string, you have to do that:

Code: Select all

put "C'est la ligne n" & quote & ":" into laLigne
Bon dimanche,

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

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: ReplaceText problem

Post by atout66 » Sun Mar 23, 2014 2:38 pm

OK Thierry, no problem.

I understood, thanks to you, the use of quotes in a string.

What I notice is that when using the special char "°" which is gotten by <Alt + 0176> it doesn't show in LC6.5.2 on my machine, despite the char "\" in front of it...

Bon dimanche as well.
Discovering LiveCode Community 6.5.2.

Post Reply