Page 1 of 1

ReplaceText problem

Posted: Fri Mar 21, 2014 4:38 pm
by Sjatplat
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

Re: ReplaceText problem

Posted: Fri Mar 21, 2014 4:49 pm
by Klaus
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

Re: ReplaceText problem

Posted: Fri Mar 21, 2014 5:03 pm
by Sjatplat
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

Re: ReplaceText problem

Posted: Fri Mar 21, 2014 5:20 pm
by Thierry
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

Re: ReplaceText problem

Posted: Fri Mar 21, 2014 5:21 pm
by Klaus
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

Re: ReplaceText problem

Posted: Fri Mar 21, 2014 5:22 pm
by Klaus
The Regex pro was a tad faster than me :D

Re: ReplaceText problem

Posted: Fri Mar 21, 2014 5:29 pm
by Sjatplat
Klaus, thanks anyway :D

Thierry, Perfect! That worked.

What does the "\" do?

Sjat

Re: ReplaceText problem

Posted: Fri Mar 21, 2014 6:53 pm
by Thierry
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

Re: ReplaceText problem

Posted: Fri Mar 21, 2014 10:01 pm
by Sjatplat
@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.

Re: ReplaceText problem

Posted: Sun Mar 23, 2014 12:26 pm
by atout66
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 ?

Re: ReplaceText problem

Posted: Sun Mar 23, 2014 12:58 pm
by Thierry
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

Re: ReplaceText problem

Posted: Sun Mar 23, 2014 2:38 pm
by atout66
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.