c1p8 thinks t1,t2,t3,t4,t5? ヽ(=^・ω・^=)丿

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
Mariasole
Posts: 235
Joined: Tue May 07, 2013 9:38 pm

c1p8 thinks t1,t2,t3,t4,t5? ヽ(=^・ω・^=)丿

Post by Mariasole » Tue Dec 10, 2013 7:16 pm

Ciao a tutti!

I am continuing to experiment! After selecting html text impossible :twisted: to do on LiveCode( http://forums.runrev.com/viewtopic.php?f=7&t=17587 ), I may have found another rock! :cry:
This time I try to replace an item with replaceText using regex :P .
I would like to eliminate the "central element" (§§§), preserving hello and world.


here is my script:

Code: Select all

  

// hello §§§ world  ------->  hello world
put "hello §§§ world" into tTemp
put replaceText(tTemp,"(?:((?:hello))((?: ))((?:§§§))((?: ))((?:world)))", $1$2$5 )  into tTemp
answer tTemp

in fact... $1$2$5 is "hello world"

because:

$1=[hello],
$2=[ ],
$3=[§§§],
$4=[ ],
$5=[world],
$0=[hello §§§ world], (???)


Is there a solution? :idea:
Thank you all!


Mariasole
(=^.^=)
or \(\=\^\.\^\=\) :lol:
Last edited by Mariasole on Sat Dec 14, 2013 3:37 pm, edited 2 times in total.
"I'm back" - The Cyberdyne Systems Model 101 Series 800 Terminator

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

Re: replaceText regex help! (=^.^=) sorry... \(\=\^\.\^\=\)

Post by SparkOut » Tue Dec 10, 2013 8:20 pm

Ciao cara Maria,

// hello §§§ world -------> hello world
put "hello §§§ world" into tTemp
replace "§§§" with empty in tTemp

-- you could also add after that:
-- replace space & space with space in tTemp

-- or simply add a space in the replace string:
-- replace "§§§ " with empty in tTemp
-- in order to deal with extra spaces
answer tTemp

--alternative method
// hello §§§ world -------> hello world
put "hello §§§ world" into tTemp
put replaceText(tTemp,"§§§","") into tTemp
-- you can add a space in the string to match to handle extra spaces too
answer tTemp


If you don't know what the pattern to match is and could be something else other than "§§§" then I'm sure there will be a way, but we'd need to see some more specific details about how the string to "clean" looks in the first place.

Mariasole
Posts: 235
Joined: Tue May 07, 2013 9:38 pm

Re: replaceText regex help! (=^.^=) sorry... \(\=\^\.\^\=\)

Post by Mariasole » Tue Dec 10, 2013 8:29 pm

Thank you SparkOut! :D
Your solutions work fine! But the problem is something else... :(
In fact, I would like to know how to solve the problem in "regex" :oops: , to apply it to more complex problems :oops: .
I would like to know whether you use the $1$2 (etc) in PERL can also be used in the syntax of LiveCode!
Grazie caro SparkOut!

Mariasole
\(\=\^\.\^\=\)
"I'm back" - The Cyberdyne Systems Model 101 Series 800 Terminator

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

Re: replaceText regex help! (=^.^=) sorry... \(\=\^\.\^\=\)

Post by SparkOut » Tue Dec 10, 2013 8:39 pm

replaceText is PCRE ready. What exact syntax you need might be a bit quirky but you should be able to do what you need with it. I just am not aware how exactly you want to parse the string. If you look up the dictionary entry for matchText you will see more specifically how it works regarding Perl-type syntax. matchText may actually be what you need.

for instance:

Code: Select all

// hello §§§ world -------> hello world
   put empty into t1
   put empty into t2
   put "hello §§§ world" into tTemp
   get matchText(tTemp,"(hello).*(world)",t1,t2)
   put t1 & space & t2 into tTemp
   answer tTemp
For *really* advanced stuff, there is always Thierry ;-)

http://forums.runrev.com/phpBB2/viewtop ... 375#p91375

Mariasole
Posts: 235
Joined: Tue May 07, 2013 9:38 pm

Re: replaceText regex help! (=^.^=) sorry... \(\=\^\.\^\=\)

Post by Mariasole » Wed Dec 11, 2013 3:35 pm

Thanks for your help SparkOut!
You are very kind, indeed, a hero like PerlMan (Thierry docet :D ).
I tried your solution and it works! Now, however, if I write a "regex" a bit more complex, the parsing does not work anymore! You could look at where am I wrong? :roll:
In fact the result is "hello w", not "hello world"! :oops:

Code: Select all

   // hello §§§ world -------> hello world
   put "hello §§§ world" into tTemp
   get matchText(tTemp,"(?:((?:(?:[[:alpha:]]{1,5}?)))((?: ))((?:§§§))((?: ))((?:(?:[[:alpha:]]{1,5}?))))", t1,t2,t3,t4,t5)
   put t1 & t2 & t5 into tTemp //t1=hello; t2=space; t5=world
   answer tTemp

Un bacio!!!

Mariasole
\(\=\^\.\^\=\) again!
"I'm back" - The Cyberdyne Systems Model 101 Series 800 Terminator

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

Re: replaceText regex help! (=^.^=) sorry... \(\=\^\.\^\=\)

Post by SparkOut » Wed Dec 11, 2013 7:07 pm

Cara Maria,

I thought for a moment we would need to call on Thierry, but... :)

You have an extra ? at the end of the capture for t5.

Code: Select all

get matchText(tTemp,"(?:((?:(?:[[:alpha:]]{1,5}?)))((?: ))((?:§§§))((?: ))((?:(?:[[:alpha:]]{1,5}))))", t1,t2,t3,t4,t5)
seems to work here for me, I just wish I knew more how your data looked for real. And I wish I knew more about regex.

Mariasole
Posts: 235
Joined: Tue May 07, 2013 9:38 pm

c1p8 thinks t1,t2,t3,t4,t5? ヽ(=^・ω・^=)丿 .

Post by Mariasole » Sat Dec 14, 2013 3:37 pm

Caro e gentilissimo (e paziente!) SparkOut,
thank you so much for the help! In this way it works, but I'm still at the beginning of the of the replacement of replaceText $1$2$3$4 :evil: (do you remeber?) :wink:

Now in fact I would take the positions of the variables (t1,t2,t3,t4,t5). Then I read the dictionary and here is the nice matchChunk function!!!

"The matchChunk and matchText functions return the same value, given the same string and regularExpression. The difference between the two is that the matchChunk function records the positions of matched substrings in the optional positionVarsList variables, while the matchText function records the substrings themselves" WOW :P

And so here's the script with matchText:

Code: Select all


 // hello §§§ world -------> hello world
   put "blabla blablablabla bla bla hello §§§ world blaablaa" into tTemp
   get matchText(tTemp,"(?:((?:(?:[[:alpha:]]{1,5})))((?: ))((?:§§§))((?: ))((?:(?:[[:alpha:]]{1,5}))))", t1,t2,t3,t4,t5)
   put t1 & " " & t2 & " " & t3 & " " & t4 & " " & t5   into tTemp
   answer tTemp

the result is:

hello §§§ world :D


And here's the (same) script with matchChunk :

Code: Select all


 // hello §§§ world -------> hello world
   put "blabla blablablabla bla bla hello §§§ world blaablaa" into tTemp
   get matchChunk(tTemp,"(?:((?:(?:[[:alpha:]]{1,5})))((?: ))((?:§§§))((?: ))((?:(?:[[:alpha:]]{1,5}))))", t1,t2,t3,t4,t5)
   put t1 & " " & t2 & " " & t3 & " " & t4 & " " & t5   into tTemp 
   answer tTemp


So I expect that by replacing "MatchText" to "MatchChunk" values ​​become:

t1=29 (h of hello)
t2=34 (space after hello)
t3=35 (§ after space)
t4=39 (space after §§§)
t5=40 (w of world)


the result is:

29 33 34 34 t5

29 33 34 34 t5 :evil: ????????

It's a bug or my boundless ignorance? :oops:


Thank you for the patience that you and others in the forum have with us beginners... :)


Mariasole
ヽ(=^・ω・^=)丿
t1,t2,t3,t4,t5
"I'm back" - The Cyberdyne Systems Model 101 Series 800 Terminator

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

Re: c1p8 thinks t1,t2,t3,t4,t5? ヽ(=^・ω・^=)丿

Post by SparkOut » Sat Dec 14, 2013 5:12 pm

I would love to help more cara Maria, but I am at the edge of my knowledge of regex. We need Thierry!

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

Re: c1p8 thinks t1,t2,t3,t4,t5? ヽ(=^・ω・^=)丿

Post by Thierry » Sat Dec 14, 2013 6:33 pm

Hello Maria,

Sparkout wakes me up to come here :)

Ok, some thoughts:

When testing the result of your regex, it would help to be a bit more precise,
so you should have seen that your code with matchText() doesnt' exactly returns what you think.

So you could try this one, instead:

Code: Select all

on mouseUp
   get matchText(tTemp,"(?:((?:(?:[[:alpha:]]{1,5})))((?: ))((?:§§§))((?: ))((?:(?:[[:alpha:]]{1,5}))))", t1,t2,t3,t4,t5)

   answer format("t1: %s t2: %s t3: %s t4: %s t5: %s",  t1,t2,t3,t4,t5)
end mouseUp
Now, about the matchChunk(); you have missed in the dictionnary:
If the regularExpression includes a pair of parentheses, the position of the substring matching the part of the regular expression inside the parentheses is placed in the variables in the positionVarsList. The number of the first character in the matching substring is placed in the first variable in the positionVarsList, and the number of the last character is placed in the second variable. Additional starting and ending positions, matching additional parenthetical expressions, are placed in additional pairs of variables in the positionVarsList.
Therefore, either get rid of your t5 variable, or add a t6 one; like this one:
(See how I check the results of the regex; no surprises then)

Code: Select all

on mouseup
   get matchChunk(tTemp,"(?:((?:(?:[[:alpha:]]{1,5})))((?: ))((?:§§§))((?: ))((?:(?:[[:alpha:]]{1,5}))))", t1,t2,t3,t4,t5,t6)

   answer format("t1..t2: <%s>\nt3..t4: <%s>\nt5..t6: <%s>", char t1 to t2 of tTemp, \
         char t3 to t4 of tTemp, char t5 to t6 of tTemp )
end mouseUp

And now, as I'm not really sure what you want to achieve; I assume
you are interested to catch the 2 words around your "§§§" string.
If this is true, then all your regex could be written this way:

Code: Select all


local tTemp = "blabla blablablabla bla bla hello §§§ world blaablaa"

   get matchChunk(tTemp,"(\w+)\s§§§\s(\w+)", t1,t2,t3,t4)
   
   answer format("t1..t2: <%s>\nt3..t4: <%s>", \
         char t1 to t2 of tTemp, char t3 to t4 of tTemp )


   get matchText(tTemp,"(\w+)\s§§§\s(\w+)", t1,t2)

   answer format("t1: %s t2: %s",  t1,t2)

Does it make sense?

Regards,

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

Mariasole
Posts: 235
Joined: Tue May 07, 2013 9:38 pm

Re: c1p8 thinks t1,t2,t3,t4,t5? ヽ(=^・ω・^=)丿

Post by Mariasole » Sun Dec 15, 2013 4:32 pm

Caro SparkOut thank you for your help and for materialized Thierry the PERLMan! :P
Thierry thanks for the impeccable lesson :mrgreen: , my abyss of ignorance has emptied a glass :oops: !
A little gift art for you, my PERLman!

Grazie e baci a SparkOut e Thierry
Mariasole
ヽ(=^・ω・^=)丿
Attachments
Thierry_PerlMan_c1p8.jpg
C1P8 thinks PERLMan
"I'm back" - The Cyberdyne Systems Model 101 Series 800 Terminator

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

Re: c1p8 thinks t1,t2,t3,t4,t5? ヽ(=^・ω・^=)丿

Post by Thierry » Sun Dec 15, 2013 8:33 pm

C1P8 thinks PERLMan
May the Regex Force be with you!

and enjoy the coming hollidays :)

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

Post Reply