Using Regular Expressions in OpenInsight Coding

Regular Expressions (also known as RegExp) are short and often complex pattern matching statements that can do the work of an entire pattern matching function. This blog post provides an example of how to harness regular expressions from inside OpenInsight BASIC+ code.

In this example we need to find dictionary table names belonging to the application while excluding system and O4W related tables. The goal is to find all table names that begin with DICT. but not DICT.SYS* or DICT.O4W*. This could be accomplished with several IF statements or a single complex IF statement but for this example a regular expression will do nicely and keep the code easy to read. This is an example snippet of the pattern matching code to check each table name against the regular expression.

*Match any table that begins with DICT.
*But exclude tables beginning with DICT.O4W and DICT.SYS
SearchPattern = "^DICT\.(?!O4W|SYS)"

*Loop through all attached tables
For TableLup = 1 To DCOUNT(@TABLES, @FM)

	*Check if the current table name matches the pattern
	isMatch = CS_REGEXMATCH(@TABLES<TableLup>, SearchPattern)
	If isMatch == True$ Then
		*GoSub processing logic here
	End
	
Next

The regular expression is stored in the variable SearchPattern and will match any string beginning with DICT. unless the string begins with DICT.SYS or DICT.O4W. The regular expression and the table name are passed into CS_REGEXMATCH which is a wrapper around a VB Script function for checking regular expressions. 

Function CS_REGEXMATCH(Search, Pattern, MatchCase)
/************************************************************************************************
* Name       :  CS_REGEXMATCH
*
* Description:  A simple wrapper around the Windows Scripting Host RegExp object
*				For testing if the supplied string matches the specified pattern.
*               For further reference see http://msdn.microsoft.com/en-us/library/ms974570.aspx
*
*              
* Parameters:
*   Search		String to test if it matches the regular expression.
*	Pattern		A regular expression string that will be applied to Search string.
*	MatchCase	Specify True$/False$ if RegEx search is case sensitive. Defaults to False$
*
* Returns:
*   True$		Search string matches pattern.
*	False$		No match.
*	""			Empty string if search error.	
*
* History (Date, Initials, Notes)
* 12/20/14    JAB    Jared Bratu, Congruity Service
*
************************************************************************************************/

$Insert Logical

*Store the WSHScript regular expression code in a common to prevent initializing it every call.
common /csre_Com/csre_WScript, csre_init%

If Assigned(search) Else Return ""
If Assigned(pattern) Else Return ""
If Assigned(matchcase) Else MatchCase = False$ ;* Default to case insensitive matching.

If Assigned(csre_init%) Else csre_init% = False$

*Is this the first call?
If csre_init% = False$ Then
	*First call, Initialize the RegEx object
	
	*Create new OLE Object and set it up.
   csre_WScript = oleCreateInstance("ScriptControl")
   OlePutProperty(csre_WScript, "Language", "VBScript")
   
   *Prepare a simple VB Script to test the supplied search string and pattern.
   *Script has two functions, RegExMatch_CaseIns() and RegExMatch()
	s = ''
	s<-1> = 'Dim r : Set r = new regexp'
	s<-1> = 'Function RegExMatch_CaseIns(s,p)'	
	s<-1> = 'r.IgnoreCase = true : r.Pattern = p'
	s<-1> = 'RegExMatch_CaseIns = r.Test(s)'
	s<-1> = 'End Function'
	s<-1> = 'Function RegExMatch(s,p)'	
	s<-1> = 'r.IgnoreCase = false : r.Pattern = p'
	s<-1> = 'RegExMatch = r.Test(s)'
	s<-1> = 'End Function'
	
	*Swap the field marks with line feeds and add it the WScript object
   Swap @FM With Char(13) : CHar(10) In S
   x = csre_WScript->AddCode(s)
   
   *Mark the object as being initialized
   csre_init% = True$
end

*WScript object has been initialized. Call the VB Script function with our parameters.

*Decide if the search is case sensitive and call appropriate function.
If MatchCase Then
	RetVal = csre_WScript->Eval('RegExMatch("' : SEARCH : '","' : PATTERN : '")')
End Else
	RetVal = csre_WScript->Eval('RegExMatch_CaseIns("' : SEARCH : '","' : PATTERN : '")')
End

*Normalize the VB Script return value.
If RetVal = "-1" Then 
	RetVal = True$ 
End Else
	RetVal = False$ 
End

Return RetVal
*
*End Function
*

The technique of using built-in VB Script functions has been demonstrated numerous times on the Revelation Software discussion forums and is a powerful method to utilize external libraries that are not part of BASIC+ or easily accessible COM APIs.

Being able to use Regular Expressions in BASIC+ code opens up new resources for validating strings such as the library of Regular Expressions at RegExLib.com where you can harness pre-built regular expressions for validating input.

Share this post

Leave a comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.

About Us

Congruity Service is a technology solutions company bringing the best technology solutions to OpenInsight projects, Drupal sites, servers, networks, and your technology needs.