Share |

Marcus' Macro Blog

Mostly tips, tutorials, articles and news about Macro Scheduler & Windows Automation
February 26th, 2009 by Marcus Tettmar

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: freddy@mjtnet.com"),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: freddy@mjtnet.com
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: freddy@mjtnet.com
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.

Related posts:

  1. Upgrades are 60% off Regular Prices
  2. Get Internet IP Address
  3. Scraping Data From Web Pages
  4. Letting off Steam?

5 Responses to “Sneak Peak: Simplified Regular Expression Support”

  1. jpuziano says:

    Hi Marcus,

    In the EasyPatterns link, there is no mention of backreferences ( see http://www.regular-expressions.info/brackets.html ) but I noticed the following section:

    - Grouping text and capturing text for use in a replace string

    Will the new MS RegEx command be able to handle replacements as well?

    I hope so… and if so… could you provide an example of what this will look like?

    I am looking forward to this one!

  2. Dick says:

    Unbelievably cool! No wonder you’ve been so quiet this week.

  3. Bob Hansen says:

    Curious, can this be done with a single Search/Replace, or will it require 12 versions, one for each month?

    To change date format from
    FROM: 18 de Febrero de 1981
    TO: YYYY/MM/DD

    I guess with Macro Scheduler, we could loop through an array to do this. Just use RegEx to rearrange the data, then run a Loop to Replace month name with month MM. Does that sound right?

    PS, I made a reply earlier, but am not seeing it. I hope this one will be more successful.

  4. Bob Hansen says:

    For the third time, I tried to post a 23 line reply, but does not show up here. I will send as PM to Marcus instead.

  5. @jpuziano – yes, certainly as far as regular Perl RegEx is concerned the command will work with back references. RegEx will also let you do replacements with an optional replace parameter. As for how/whether EasyPatterns handles back references I will need to refer to DataMystic on that.

    @Bob Hansen – as above, yes, we will provide a way to do replacements.

Leave a Reply