View Full Version : isValidRegexPattern function ??
01-15-2009, 09:47 AM
I'm trying to determine if a given string is a valid regex pattern but i can't seem to find any function that will let me do so. The only alternative I've found is simply try/catch but surely there must be a neater solution. Anyone know of one ?
01-15-2009, 11:24 AM
try/catch is the way to handle actual pattern checks, near's I can tell; quick search through MSDN shows them using that exact method anytime they've got something like an expression validator (to see if an expression matches a string or not) - http://msdn.microsoft.com/en-us/library/aa720492(VS.71).aspx - they use try/catch as well.
01-15-2009, 12:02 PM
well that's lame/lazy imo... oh well this will do then: fn isValidRegexPattern pattern =
regex = dotNetClass "System.Text.RegularExpressions.RegEx"
try(regex.isMatch "test" pattern;true)catch(false)
01-15-2009, 12:15 PM
Well, it seems lame from a typical maxscript try/catch because when we tend to use a try/catch, it's to catch any actual exceptions that might occur.. i.e. trap for code that not just -would- throw a full-blown error, but actually -does- throw that error.
With most .net methods, there's a bunch of internal checking to see whether things would throw an error before they are actually executed.. if it's determined that indeed an error would get thrown, it calls an internal throw() routine itself. It's pretty light-weight, so not much of an issue.
MaxScript can do much the same with throw() as well.. you can make a function that validates input, for example, and call throw() if the inputs are incorrect.. it's just that we typically do something like return false or undefined instead (if doing any checking at all), allowing whatever calling code to continue on merrily / check for a false/undefined value to deal with it. throwing an error would actually be more appropriate in such cases :)
01-15-2009, 08:16 PM
yea i guess you're right. ah well crisis over :) Thanks again!
01-16-2009, 10:22 AM
Try/catch is used much more in .net than MXS, as there is a dedicated exception class that can be used to provide a developer more control over what to do with a certain error. (a bit like getcurrentexception() but in more detail) There may a be a few different types of error to catch in a certain point which needs to be handled in different ways, for example Null Exceptions, Stack Overflows or Out of Memory exceptions.
.net also has a 'finally' clause in try/catch, which can be used to clear up any programming detritus after an exception has happened.
The MXS help mentions try/catch is bad practise due to speed issues, in reality most of the time its not a problem to me if something takes 43ms rather than 17ms, but I can see there would be times where it would matter.
So in .net its not considered bad practise and most of the examples I see are setup this way.
01-16-2009, 10:22 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2015, Jelsoft Enterprises Ltd.