preload

1-1 Comparison: MatchIn / MatchFor

Posted by Albert Gareev on Jul 23, 2010 | Categories: Source codeVerification

Reference Page: Comparison Rules

Automatic Comparison Rules

Relationship: One-to-One / Match In String / Match For String

Definitions

Supported data types: String, Regular Expression

 1. Match in String

First argument – base string; second argument – substring / regular expression.

2. Match for String

First argument – substring / regular expression; second argument – base string.

3. Special cases

  • Empty string doesn’t match anything but another empty string
  • “” != “.*” (empty string is not treated equally as a regex defining any/none character(s))  

Implementation

Public Function Regex_Test(ByVal strSrc, ByVal strRegEx)
 Dim objRegEx
 Dim boolRC, intRC

  If strRegEx = "" Then
    If strSrc = "" Then
    Regex_Test = True
    Else
    Regex_Test = False
    End If
  Exit Function
  End If
 
  Set objRegEx = New RegExp 
 objRegEx.Pattern = strRegEx

 On Error Resume Next
 boolRC = objRegEx.Test(strSrc)
 intRC = Err.Number
 On Error GoTo 0
 If intRC <> 0 Then boolRC = False

 Set objRegEx = Nothing
 Regex_Test = boolRC
End Function

Sample Calls

' Case "MatchIn"
    boolRC = Regex_Test(sActualValue, sExpectedValue)
' Case "MatchFor"
    boolRC = Regex_Test(sExpectedValue, sActualValue)

Test Code

  Log.Message("RegEx routines")
 
  boolRC = Regex_Test("lorem ipsum", "ore")
  If Not boolRC Then
    Log.Error("Regex_Test failed")
  End If
  boolRC = Regex_Test("lorem ipsum", "  psu")
  If boolRC Then
    Log.Error("Regex_Test failed")
  End If
  boolRC = Regex_Test("", "abc")
  If boolRC Then
    Log.Error("Regex_Test failed")
  End If
  boolRC = Regex_Test("abc", "")
  If boolRC Then
    Log.Error("Regex_Test failed")
  End If
  boolRC = Regex_Test("", "")
  If Not boolRC Then
    Log.Error("Regex_Test failed")
  End If
  boolRC = Regex_Test("lorem ipsum dre", ".*re.+")
  If Not boolRC Then
    Log.Error("Regex_Test failed")
  End If

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.