February 26, 2009

Sneak Peak: Simplified Regular Expression Support

Filed under: Announcements,Scripting — Marcus Tettmar @ 4:52 pm

I don’t know many people who find Regular Expressions easy. If the following makes no sense to you, don’t worry, you’re not alone:

([a-z0-9!#$%&’*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&’*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)

It’s actually a regular expression pattern which will match an email address in a string. I’m sure you knew that.

At present to use Regular Expressions in Macro Scheduler you have to use VBScript’s regular expression object:

VBSTART
Function RegExpTest(sEmail)
  RegExpTest = false
  Dim regEx, retVal
  Set regEx = New RegExp

  regEx.Pattern ="([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)"

  regEx.IgnoreCase = true

  Set retMatches = regEx.Execute(sEmail)
  
  If retMatches.Count > 0 then
    RegExpTest = retMatches(0).Value
  End If

End Function
VBEND

VBEval>RegExpTest("My email address is: [email protected]"),theEmail
MessageModal>theEmail

In order to simplify things we’re currently working on a native Regular Expression function called, appropriately enough, RegEx. Using this, the following code will find the email address in the given string:

Let>text=My Email Address: [email protected]
Let>pattern=([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)
RegEx>pattern,text,0,matches,num,0
MessageModal>matches_1

A bit simpler, as you don’t need to use VBScript. But you still need to use that weird and wonderful regular expression syntax.

Luckily our friends at DataMystic have created something called EasyPatterns which maps real English to regular expression syntax. Thanks to DataMystic we are able to use EasyPatterns in Macro Scheduler. Setting the EasyPatterns flag in the new RegEx command allows us to turn the above into:

Let>text=My Email Address: [email protected]
Let>pattern=[EmailAddress]
RegEx>pattern,text,1,matches,num,0
MessageModal>matches_1

Note the second line has been simplified to Let>pattern=[EmailAddress]. Nice. Now it makes sense!

Check out the EasyPatterns Reference here to find out what else you can do.

Watch out for the next Macro Scheduler maintenance update, which, all being well, will have this function included as a bonus.

Please note this post refers to work in progress. The syntax in the released version may differ slightly. I will update with changes when they happen.