by Dongbo Zhang
We recently introduced a new feature in Uniface that empowers developers with enhanced pattern matching and validation capabilities: Regex String. On top of legacy Syntax Strings, Uniface now provides you with a new option of using regular expressions to define and validate patterns within your applications.
What are Regex Strings in Uniface
Regex Strings are regular expressions enclosed in forward slashes (/). They provide a flexible and robust way to specify patterns for validating user input, searching and comparing strings, and performing complex text manipulations.
How to Use Regex Strings in Uniface
Defining Field Syntax:
You can use Regex Strings to specify the expected format of data in fields for Form and DSP (Dynamic Server Page) components. For example, to enforce a Canadian postal code format (B2Y 5K2), use the Regex String /[A-Z]\d[A-Z] \d[A-Z]\d/.
Pattern Matching in ProcScript:
In ProcScript, leverage the $regex function to convert a string to a Regex String and perform pattern matching operations. For instance:
· Searching and Replacing Strings:
Regex strings can be utilized with functions like scan, $scan, $rscan, and $replace to search for specific patterns within strings and perform efficient replacements.
variables
string sMyString
endvariables
sMyString = "Sample text with a pattern B2Y 5K2 in it"
; Check if the string contains a Canadian postal code pattern
if ($scan(sMyString, $regex("[A-Z]\\d[A-Z] \\d[A-Z]\\d")))
; Match found
; Perform further actions...
else
; No match found
; Handle accordingly...
endif
· Truncating Strings:
Use $ltrim and $rtrim with Regex Strings to trim strings based on specified patterns.
vString = $ltrim("1111234567890111uniface",$regex("\d"))
; vString now is "uniface"
· Splitting Strings:
Easily split strings into substrings using $split with Regex Strings as delimiters.
variables
string vList
string vText
string vLeft
string vRight
endvariables
vText = "All non-word,character;should\be?treated'as+a delimiter”
while (0 != $split(vText, 1, $regex("[^-\w]”), vLeft, vRight))
vText = vRight
putitem vList, -1, vLeft
endwhile
putitem vList, -1, vText
; The final vList will be a list like: All·;non-word·;character·;should·;be·;treated·;as·;a·;delimiter
Advantages of Regex Strings over Legacy Syntax Strings
While Uniface previously supported Syntax Strings for pattern matching, Regex Strings offer several advantages:
Enhanced Flexibility: Regex Strings support a wide range of complex pattern definitions.
Powerful Pattern Matching: Regular expressions allow for advanced pattern matching with features like character classes, quantifiers, and more.
Ease of Use: Regex Strings provide a more intuitive and concise syntax (Modified ECMAScript regular expression grammar) compared to legacy Syntax strings.
We encourage all Uniface developers to explore the capabilities of Regex Strings for defining and validating patterns within their applications. Regex Strings offer unmatched flexibility and precision, enabling you to create robust data validation rules and perform more sophisticated text manipulations.
Have questions or need assistance? Feel free to contact the Uniface team or visit our online documentation portal for more detailed information.
#tofp
#tofp