KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Supporting special characters in a Match regex search pattern
PRODUCT: 4D | VERSION: 11.5 | PLATFORM: Mac & Win
Published On: March 26, 2010

When using the 4D command Match Regex there may be a need to do more than simply search for literal pieces of text, there may be the need to know if a character is a punctuation or symbol.

When trying to identify punctuation or symbols you must be aware that there are certain reserved characters in RegEx for special use. In 4D, there are 7 characters with special meanings: the caret ^, the ampersand &, the dash -, the backslash \, the double-quote ", the opening square bracket [, and the closing square bracket ]. These special characters are sometimes referred to as "metacharacters".

For 4D to accept these metacharacters in a regex pattern they must be "escaped". Escaping a single metacharacter with a backslash works in most regular expression flavors. However, in 4D there is an extra step. In the 4D regex pattern the escape has to be escaped. Instead of preceding the metacharacter with a single backslash \, it must be preceded with double backslashes \\. Where, for example, in a normal 4D string to include a double-quote in the string you would escape it like this \", within a 4D regex pattern it has to be escaped like this \\".

If one of these characaters is not escaped properly, in this example the caret ^, 4D will present the following error message.



Below is a method with all the metacharacters properly escaped that will let you test a character in a string to know if it is a punctuation or symbol character or not. Note in this method the space character, " ", is included in the test. Also note how the to get the pattern to include the backslash \ as a metacharacter; the escape has to be escaped "\\\\".

If (True)
  If (False)
    `*****************************************************************************
    `//
    `//  STR_IsSymbol ( Text ) -> Boolean
    `//
    `//  Purpose:  Test a character and return true if it is Uppercase
    `//
    `//  $0 - BOOLEAN - Test result
    `//  $1 - TEXT - Character to test
    `//
    `*****************************************************************************

  End if
  C_TEXT($MethodName_T)
  $MethodName_T:=Current method name
  `=====================    Declare Variables    ==================================
  `method_parameters_declarations
  C_BOOLEAN($0;$IsSymbol_B)
  C_TEXT($Text_T;$1)
  `--------------------------------------------------------------------------------
  `method_wide_constants_declarations
  `--------------------------------------------------------------------------------
  `local_variable_declarations

  C_TEXT($Regex_T)
End if
`======================    Initialize and Setup    ================================

$Text_T:=$1

    `//  Insuring that only one character is being tested.
    `//
$Text_T:=Substring($Text_T;1;1)

`========================    Method Actions    ==================================

$Regex_T:="[ ~!@#$%\\^\\&*?_\\-`~()\\\\,\"'{|}/<>\\[:\\];.=+]"
$IsSymbol_B:=Match regex($Regex_T;$Text_T)

`========================    Clean up and Exit    =================================

$0:=$IsSymbol_B