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!
I can not get the Rexex expression to work correctly.
I am tying to find lines matching the pattern X12345Y12345 from a very long list. The pattern [X] will works, but I want to make sure I match
X followed by a 5 digit mumber then Y followed by a 5 digit number. Found a regex test web site, but Livecode version of Regex seems different.
on mouseUp
repeat with y = 1 to 10000
put "X" & random(88888) + 10000 & "Y" & random(88888) + 10000 into line y of temp
end repeat
put "X12345Y12345" into line random(10000) of temp
---actual routine starts here
put 1 into tIndex
repeat for each line tLine in temp
if tLine= "X12345Y12345" then
exit repeat
end if
add 1 to tIndex
end repeat
answer tIndex
end mouseUp
Not sure how long your list is. The list above is 10,000 lines. only a second or two was needed to find the line of interest
The entire first section is just to create a list with one valid entry somewhere in it. You can simply substitute (pseudo):
Thanks for replying.
I do not thinks this would work for what I am trying to do.
I need to find lines of data that match this general pattern. I am not trying to find specifically X12345Y12345. I need to find any line that only contains
X{5 digit number]Y{5 digit number}.
examples:
X89888Y45678
X45898Y34833
I need Regex to find all lines that match the pattern.The field I am reading also contains other information that I do not want in the final list.
on mouseUp
--insert your data into temp
put 1 into tIndex
repeat for each line tLine in temp
if char 1 of tLine = "X" and char 2 to 6 of tLine is a number and char 7 of tLine = "Y" and char 8 to 12 of tLine is a number\
and the length of tLine = 12 then
exit repeat
end if
add 1 to tIndex
end repeat
answer tIndex
end mouseUp
Here is a test handler that you can step through to see how it worked in the old days. To be fair, the "filter" command and regex itself is much more modern and faster.
on mouseUp
put "X123Y123" into line 1 of temp
put "X1234567Y123" into line 2 of temp
put "X12345Y12345" into line 3 of temp
put "X123Y1234567" into line 4 of temp
put 1 into tIndex
breakpoint
repeat for each line tLine in temp
if char 1 of tLine = "X" and char 2 to 6 of tLine is a number and char 7 of tLine = "Y" and \
char 8 to 12 of tLine is a number and the length of tLine = 12 then exit repeat
add 1 to tIndex
end repeat
answer tIndex
end mouseUp
-- Sent when the mouse is released after clicking
-- pMouseButton specifies which mouse button was pressed
on mouseUp pMouseButton
put empty into field "result"
repeat for each line tLine in field "input"
if matchtext(tLine,"[X]\d{5}") then
put tLine&cr after field "result"
end if
end repeat
end mouseUp
using 2 scrolling fields named input and result, hope it helps but this is written at 4 am
edited because I still don´t get the difference between am and pm at age 52 LOL
-- Sent when the mouse is released after clicking
-- pMouseButton specifies which mouse button was pressed
on mouseUp pMouseButton
local mText
put field "input" into mText
filter lines of mText matching regex"[X]\d{5}"
put mText into field "result"
end mouseUp
Mark Waddingham once told me there's no specific answer. Execution time depends on the content of the data, length of the data, and structure of the regex. If timing is important, you'd need to test both methods per each example.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
jacque wrote: Fri Dec 18, 2020 7:04 pm
Mark Waddingham once told me there's no specific answer. Execution time depends on the content of the data, length of the data, and structure of the regex. If timing is important, you'd need to test both methods per each example.
Exactly. It's not the algo, but the application of the algo to a certain type of problem. But they aren't random, they follow patterns.
I've done enough comparative benchmarking between arrays and chunks to have a fairly useful sense of when to use each. With enough benchmarking of regex vs chunks, similarly useful guidance may emerge.
Richard Gaskin LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
for me, 10000 lines of random text, with some hit and misses, takes 5 ticks with the filter variation, and something like 160 with the for each
but if the hits are put into a string, and after that is put into the result field it is pretty much the same
so seems to me it pretty fast, but yeah, the more lines, the slower it will go