Class: cRegEx
Properties | Events | Methods | Index of Classes
Provides functionality for pattern matching, searching, and manipulating strings using regular expressions
Hierarchy
cObject > cRegEx
Show full hierarchy and direct subclasses
- cObject
- cRegEx
Library: Common Class Library
Description
The cRegEx class provides functionality for pattern matching, searching, and manipulating strings using regular expressions. It enables developers to define complex search patterns and apply them to strings for tasks such as validation, extraction, replacement, and splitting of text.
The class supports both simple and advanced operations, such as finding matches, capturing groups, and invoking callback functions during matching or replacement operations. By leveraging the power of regular expressions, cRegEx makes string processing efficient and flexible.
Regular expressions in DataFlex follow the PCRE2 syntax. If you are new to regular expressions or need a refresher, RegEx101.com is an excellent resource.
On RegEx101.com, you can: - Experiment with your expressions interactively. - See a breakdown of the syntax and what each part of your expression does. - Learn advanced techniques like named groups, lookaheads, and backreferences.
To ensure compatibility, select PCRE2 as the syntax while testing your expressions. This matches the behavior of the cRegEx class and ensures consistent results in your application.
Using the cRegEx Class
The cRegEx class holds a single regular expression that can be configured using the psExpression property. The expression can be ran against the subject string using one of the functions. The expression is compiled on first usage (so when calling one of the functions). Changing psExpression or one of the configuration properties will dispose the compiled expression. If the expression is invalid errors will be thrown on first usage.
Use one of the Match functions (Match, MatchAll, MatchAllOffsets, MatchAllGroups, MatchAllCallback) to find and process matches inside a string. Use the Split function to split a string into an array of smaller strings based on the matches. The Substitute, SubstituteAll and SubstituteExCallback functions can be used to replace the matches inside the string.
Sample
The sample below shows matches XML characters in the sSubject string.
Object oMatchXmlChars is a cRegEx
Set psExpression to "&\S+?;"
End_Object
String sSubject
Integer iFirstPos iLastPos
String[] aMatches
Move "Value is < than "e32"e" to sSubject
Get Match of oMatchXmlChars sSubject to iFirstPos // 10
Get RMatch of oMatchXmlChars sSubject to iLastPos // 29
Get MatchAll of oMatchXmlChars sSubject to aMatches // "<", ""e", ""e"