isValidRegexPattern function ??

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  01 January 2009
isValidRegexPattern function ??

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 January 2009
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) - - they use try/catch as well.
  01 January 2009
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 January 2009
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 January 2009
yea i guess you're right. ah well crisis over Thanks again!
  01 January 2009
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.

Read my technical blog -
  01 January 2009
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 03:24 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.