Page 1 of 2

Simple MatchChunk question

Posted: Wed Nov 24, 2021 9:55 am
by kaveh1000
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

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 10:29 am
by richmond62
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.

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 10:31 am
by kaveh1000
From what I remember, this should be looking at the offset of "e" inside "Hello", so tStart should return as 2.

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 10:48 am
by richmond62
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.

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 10:55 am
by bn
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

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 11:06 am
by richmond62
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.

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 11:14 am
by bn
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

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 11:20 am
by kaveh1000
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

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 11:24 am
by kaveh1000
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

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 11:29 am
by bn
Hi Kaveh,

you are cheating by using regex. :)

Kind regards
Bernd

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 11:33 am
by richmond62
The Dictionary needs a spot of editing.
"([k])"
But for the right sort of brackets and their order a program was lost. :(

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 1:34 pm
by kaveh1000
@Bernd: But MatchChunk is specifically for regex I think. ;-)

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 2:07 pm
by bn
@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

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 2:22 pm
by kaveh1000
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.

Re: Simple MatchChunk question

Posted: Wed Nov 24, 2021 4:54 pm
by dunbarx
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