Exiting matchChunk() gracefully with invalid regex entry

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
kaveh1000
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 220
Joined: Sun Dec 18, 2011 7:23 pm
Location: London
Contact:

Exiting matchChunk() gracefully with invalid regex entry

Post by kaveh1000 » Thu Nov 22, 2018 9:45 am

I have a handler that takes regex input from a user and uses it to find text in a field. The user puts a regex in a box. This is the_search. In a handler I have:

Code: Select all

   put "(" & the_search & ")" into the_search
   get matchChunk(fld "text", the_search, the_start, the_end)
 
The first line puts in the parenthesis so I can find the position of the item found.

The problem arises when the user puts in an invalid regex, e.g. abc [0. I get an execution error at matchChunk. I want something to simply exit the handler if there is erroneous entry. Any ideas pls?
Kaveh

Klaus
Posts: 10974
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Exiting matchChunk() gracefully with invalid regex entry

Post by Klaus » Thu Nov 22, 2018 10:32 am

Hi Kaveh,

can't you just TRY this?

Code: Select all

...
put "(" & the_search & ")" into the_search
try
  get matchChunk(fld "text", the_search, the_start, the_end)
catch errNum
  answer "Idiot!"
end try
...
Best

Klaus

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

Re: Exiting matchChunk() gracefully with invalid regex entry

Post by kaveh1000 » Thu Nov 22, 2018 3:00 pm

Klaus does it again! Thank you. Yes, I had forgotten "try". One of these days, RTFM again. ;-)
Kaveh

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

Re: Exiting matchChunk() gracefully with invalid regex entry

Post by kaveh1000 » Thu Nov 22, 2018 6:46 pm

As an added bonus, if there is indeed an error in matchChunk, I am setting the backgroundcolor of the regex field to a colour, so there is real time feedback that the text is not a valid regular expression. :-)
Kaveh

Post Reply

Return to “Talking LiveCode”