Simple MatchChunk question

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Simple MatchChunk question

Post by kaveh1000 » Wed Nov 24, 2021 9:55 am

Can someone tell me why the following code does not work? I get empty for tStart and tEnd. Should I not get the offset of "e"?

Code: Select all

on mouseup
   put empty into tStart; put empty into tEnd
   get matchChunk("Hello", "e", tStart, tEnd)
   answer tStart && tEnd
end mouseup
Kaveh

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
Joined: Fri Feb 19, 2010 10:17 am

Re: Simple MatchChunk question

Post by richmond62 » Wed Nov 24, 2021 10:29 am

Well, I tried some code of my own:

Code: Select all

on mouseUp
   put "my socks are yellow" into rmSTRING
   get matchChunk (rmSTRING, "k", xStart, xEnd)
   answer xStart && xEnd
end mouseUp
and got NOTHING.

Also with this:

Code: Select all

on mouseUp
   --put "my socks are yellow" into rmSTRING
   get matchChunk ("my socks are yellow", "k", xStart, xEnd)
   answer xStart && xEnd
end mouseUp
Which surprised me given what the Dictionary says.
-
SShot 2021-11-24 at 11.40.55.png
-
But, then I had a thought and tried this:

Code: Select all

on mouseUp
   put "my socks are yellow" into fld "fff"
   get matchChunk (fld "fff", "k", xStart, xEnd)
   answer xStart && xEnd
end mouseUp
and that didn't work either.

Nor did it work in LiveCode 8.2.0 dp-2.
Last edited by richmond62 on Wed Nov 24, 2021 10:45 am, edited 1 time in total.

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Simple MatchChunk question

Post by kaveh1000 » Wed Nov 24, 2021 10:31 am

From what I remember, this should be looking at the offset of "e" inside "Hello", so tStart should return as 2.
Kaveh

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
Joined: Fri Feb 19, 2010 10:17 am

Re: Simple MatchChunk question

Post by richmond62 » Wed Nov 24, 2021 10:48 am

From what I remember
Where do you remember this from as it has obviously been a "dead duck" for quite some time?

I think you may be doing something wrong, because if I do this:

Code: Select all

on mouseUp
   --put "my socks are yellow" into rmSTRING
   put matchChunk ("my socks are yellow", "k", xStart, xEnd)
   --answer xStart && xEnd
end mouseUp
The messageBox returns TRUE.

And if I do this:

Code: Select all

on mouseUp
   --put "my socks are yellow" into rmSTRING
   get matchChunk ("my socks are yellow", "k", xStart, xEnd)
   put xStart && xEnd
   --answer xStart && xEnd
end mouseUp
I get nothing at all.

This is probably because the variable xStart should contain the number
of the char in the string where the matchChunk should start looking.
Last edited by richmond62 on Wed Nov 24, 2021 11:00 am, edited 1 time in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Simple MatchChunk question

Post by bn » Wed Nov 24, 2021 10:55 am

Hi Kaveh,

matchChunk works also with regular expressions.

I think what you need is a regular expression if you want the offset of the char.

From the dictionary:
matchChunk(field "Title",myExpr,startMatch,endMatch)
The code below works in your example. But I am notoriously bad at regex.

Code: Select all

on mouseup
   put empty into tStart; put empty into tEnd
   get matchChunk("Hello", "([e])", tStart, tEnd)
   answer tStart && tEnd
end mouseup
I think only Thierry could really help here.

Kind regards
Bernd

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
Joined: Fri Feb 19, 2010 10:17 am

Re: Simple MatchChunk question

Post by richmond62 » Wed Nov 24, 2021 11:06 am

Interestingly enough:

Code: Select all

on mouseUp
   put empty into xStart;  put empty into xEnd
   get matchChunk ("my socks are yellow", "[(k)]", xStart, xEnd)
   answer xStart && xEnd
end mouseUp
Does NOT work.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Simple MatchChunk question

Post by bn » Wed Nov 24, 2021 11:14 am

Interestingly enough this does work

Code: Select all

on mouseUp
   put empty into xStart;  put empty into xEnd
   get matchChunk ("my socks are yellow", "([k])", xStart, xEnd)
   answer xStart && xEnd
end mouseUp
Kind regards
Bernd

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Simple MatchChunk question

Post by kaveh1000 » Wed Nov 24, 2021 11:20 am

OK, I got it. We need to grab the item we are interested in with brackets. so:

Code: Select all

on mouseup
   put empty into tStart; put empty into tEnd
   get matchChunk("Hello", "(e)", tStart, tEnd)
   answer tStart && tEnd
end mouseup
returns

Code: Select all

2,2
Kaveh

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Simple MatchChunk question

Post by kaveh1000 » Wed Nov 24, 2021 11:24 am

Going further,

Code: Select all

on mouseup
   get matchChunk("Hello", "(el).*(o)", tOne, tTwo, tThree, tFour)
   answer tOne && tTwo && tThree && tFour
end mouseup
produces

Code: Select all

2 3 5 5
Kaveh

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Simple MatchChunk question

Post by bn » Wed Nov 24, 2021 11:29 am

Hi Kaveh,

you are cheating by using regex. :)

Kind regards
Bernd

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10098
Joined: Fri Feb 19, 2010 10:17 am

Re: Simple MatchChunk question

Post by richmond62 » Wed Nov 24, 2021 11:33 am

The Dictionary needs a spot of editing.
"([k])"
But for the right sort of brackets and their order a program was lost. :(

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Simple MatchChunk question

Post by kaveh1000 » Wed Nov 24, 2021 1:34 pm

@Bernd: But MatchChunk is specifically for regex I think. ;-)
Kaveh

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: Simple MatchChunk question

Post by bn » Wed Nov 24, 2021 2:07 pm

@Kaveh,

for me some of the examples did not look like regular expressions but the generic description of matchChunk in the dictionary
matchChunk(string, regularExpression [, positionVarsList])
suggests so. (I said I was guessing at regex) But I stay away from matchChunk but use charOffset instead.

Kind regards
Bernd

kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 539
Joined: Sun Dec 18, 2011 7:23 pm
Contact:

Re: Simple MatchChunk question

Post by kaveh1000 » Wed Nov 24, 2021 2:22 pm

Ah, but I have varying text, so e.g. in the following XML text:

Code: Select all

                 <Measure>
			<MeasureType>08</MeasureType>
			<Measurement>2137</Measurement>
			<MeasureUnitCode>gr</MeasureUnitCode>
		</Measure>
		<Measure>
			<MeasureType>01</MeasureType>
			<Measurement>234.0</Measurement>
			<MeasureUnitCode>mm</MeasureUnitCode>
		</Measure>
I need to find each chunk inside the <Measure> tags. Copy one, then the other. only regex will allow me to do that.
Kaveh

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: Simple MatchChunk question

Post by dunbarx » Wed Nov 24, 2021 4:54 pm

only regex will allow me to do that.
Not sure that is true. You could certainly use "ordinary" LC with its offset functions, then find the end tag, and put it all together, but these certainly are not as compact as regex.

Craig

Post Reply