Syntax Strings for Pattern Matching

Uniface enables you to determine if the data in a string value matches a desired pattern using syntax strings. A syntax string is a group of characters and syntax codes (#, *, &, @, ~, ?, (, ), %, and ^) enclosed in single quotation marks (').

Syntax strings can be used to specifying a field syntax, such as postal code or telephone number. They are also useful when searching for and comparing or replacing strings. For example, they can be used when:

  • Defining logical expressions with the =, ==, and != operators in conditional ProcScript statements (if, repeat, selectcase, or while).

    Note:  In a logical expression, the pattern specified by the syntax string must occur within the first 256 characters of the string with which it is being compared.

  • Searching and replacing strings using scan, $scan, $rscan, $replace.
  • Truncating strings using $ltrim and $rtrim

Using Syntax Strings

To represent the pattern of a Canadian postal code (such as B2Y 5K2), the syntax string would be &#& #&#, where the syntax code # represents a number and & a letter.

To enforce this pattern in a data entry field, you can specify this syntax string in the Field Syntax property.

In ProcScript, you can use $syntax to convert a string to a syntax string, and use this to search for a matching string. For example:

;Field ADDRESS = "Mary John, 2 Ginger Lane, Genoa NS B0R 1N6"
; Scan the string for a substring that matches the postal code pattern, 
; and return the position of the matching string

vLocation = $scan (ADDRESS, $syntax(&#& #&#))

; If a position is returned, issue a message

if (vLocation > 0)
 putmess = "A postal code is provided"
endif

Syntax of Syntax Strings

In ProcScript you can enter syntax strings using the correct syntax, or use the ProcScript function $syntax to convert a string to a syntax string.

Syntax: '{[Mode]}PatternString{[ExitMode]}'

  • Mode—syntax mode, which determines how the subsequent characters will be handled. If omitted or set to %[X] or %[Exit], the syntax characters (#, *, &, @, ~, ?, (, ), %, and ^) are interpreted and all other characters are treated as constants. If the Mode is set to any other value, all characters that follow are treated as constants, including the characters that are normally treated as syntax characters.
  • PatternString—string of characters, which can include the following syntax codes: #, *, &, @, ~, ?, (, ), %, and ^.
  • ExitMode—exit mode %[X] or %[Exit], which turns off the mode that was previously set.

The maximum length of a syntax string is 127 characters.

Syntax Codes

Syntax Codes for Pattern Matching
Syntax code Explanation
# One digit (0-9)
#* 0-n digits
& One letter (A-Z, a-z)
&* 0-n letters
@ One letter, digit, or underscore (_)
@* 0-n letters, digits, or underscores
Extended Characters. These are determined by the value of the $EXTENDED_SYNTAX assignment setting. Default is the full Unicode character set. For more information, see $EXTENDED_SYNTAX.
~& One extended letter.
~&* 0-n extended letters
~@ One extended letter, digit, or underscore (_)
~@* 0-n characters, extended letters, digits, or underscores

ASCII Characters

? One ASCII character
?* 0-n ASCII characters
A through Z That letter, in uppercase

a through z

That letter, in uppercase or lowercase
x The ASCII character x, except the syntax codes (#, *, &, @, ~, ?, (, ), %, and ^)
%x The ASCII character x, with no special meaning
(any) The syntax string (variable or constant) contained in parentheses is optional. A syntax check is done only if data is present.
%%^ Carriage return or line feed.

Note:  This code is allowed only to define entry formats in a field's syntax definition. It is not used in Proc pattern matching.

To search for a character that is used as a syntax code, you must set a syntax mode or precede it with a percent sign (%). Thus, to search the literal percent sign, you can enter %%.

The fields and variables used in conjunction with syntax strings must be data type string. If you need to use another type of data (Numeric or Time, for example), first convert this to a string by substituting the value into a constant string, for example "%%DATE1".

Syntax Modes

Syntax modes determine how characters and syntax codes are treated when performing a pattern-matching search.

Syntax String Modes
Syntax String Mode Meaning Example Resulting Syntax String Matches
Classic

X

The following characters in String are treated as syntax codes for pattern-matching: # * & @ ~ ? ( ) % ^

This is the default, and corresponds to behavior prior to Uniface 9.4.01

$syntax("D&G") '%[X]D&G' DOG, DIG, etc.
CaseSensitive

CS

S

Match only characters with the same case and treat syntax code characters as literals, not codes

$syntax("D&G", "CS")

'%[CS]D%&G%[X]' D&G
CaseInSensitive

CI

I

Match all characters irrespective of case, and treat syntax code characters as literals, not codes

Treat the characters that follow as case-insensitive.

$syntax("D&G", "CI")

'%[CI]D%&G%[X]' D&G, d&g, D&g, d&G
NlsLocale

NLS

N

Match all characters irrespective of case according to the locale, and treat syntax code characters as literals, not codes

$syntax("i#B", "NLS")

'%[NLS]i%#B%[X]' Depends on locale.

If $NlsLocale= tr_TR, matches: i#B and İ#b, but not I#B

The following ProcScript shows how a syntax string can be used in a conditional expression.

if (vValue = '#')
   message "%%vValue contains a digit."
endif

The table contains shows the result of various syntax strings, given that vValue = 123

ProcScript with Syntax String

Result

if ('#' = "123")

if ('#*' = "123")

FALSE

TRUE

if ('#*' = vValue)

if ('#*' = "%%vValue")

TRUE

TRUE

if ('&###' = "1234")

if ('@###' = "1234")

FALSE

TRUE

if ('?' = "A")

if ('??' = "A")

if ('??*' = "A")

if ('?' = "ABC")

if ('??*' = "ABC")

TRUE

FALSE

TRUE

FALSE

TRUE

if ('(#(-))&&&' = "ABC")

if ('(#(-))&&&' = "1ABC")

if ('(#(-))&&&' = "1-ABC")

if ('(#(-))&&&' = "12ABC")

TRUE

TRUE

TRUE

FALSE

Related Topics