Implementing preg_replace in VBScript


IT blog

Here are a couple of wrapper VBScript functions that make using script’s built-in RegExp class similar to PHP’s preg_xxx family of functions.

First a private helper function to initialize an instance of RegExp:

This enables optionally to set flags of the object in the search string after the trailing slash e.g. “/test/mi” will search for “test” ignoring case and multi-line. This helper function is not meant to be used by the client scripts but helps implement the actual search&replace functions.

Next the implementation of preg_match, preg_replace and preg_split becomes very simple

These are fairly straight forward and are about enough to complete 99% of any search&replace task at hand. For preg_replace one can use replace placeholders in VBScript notation ($1, $2, etc.) to match search groups.

The troublesome 1% with this implementation of preg_replace is that it cannot handle callback functions for replace_arg. So here is a…

View original post 214 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s