As you may know we have Regular Expression classes for many years in our Xojo plugins. See RegExMBS class for the old interface using the older PCRE library. Those classes will stay available for the future, but we mark them as deprecated. The library is no longer in development and only gets security related fixes.
For PCRE2 we made new plugins classes:
The PCRE2CompilerMBS parses a pattern and creates the PCRE2CodeMBS object with the compiled version of that pattern. Unless configured otherwise this includes running the Just-In-Time compiler to convert your pattern to a little native piece of program code.
Once you have the code, you can create a PCRE2MatchDataMBS object to hold the results of a search. You can now loop over text to find all positions where your pattern matches. The data object is reused. We have convenience functions to perform the search and return you a new data object or an array with match data objects for all the found positions.
The PCRE2MatchContextMBS class can be used to configure limits in matching like the amount of memory allowed to be used. All error handling is translated to exceptions with the PCRE2ExceptionMBS class. Please check results of functions since not finding something doesn’t cause an exception, but returns a negative number for the number of matched captures.
If you like you can check the various flags to configure the search. This may include multi line, case sensitively, whole word and other useful flags. The library is unicode aware, so you can find unicode characters. If you like, you can also check the compiled pattern for what parameters it contains and enumerate all the callouts. Compiled patterns can be stored serialized for later reuse. This further reduces the CPU time to prepare them for reuse.
Finally we have a Substitute() function to replace a found pattern with a replacement pattern. This new pattern can then reference captured values from the search pattern. If you set the SubstituteGlobal flag, we perform a ReplaceAll for a whole text.
Our new RegEx plugin is already compiled for all 15 platforms: macOS 32 and 64-bit Intel, macOS Apple Silicon, iOS for Device and Simulator, Windows 32 and 64-bit for Intel and 64-bit ARM, Linux in 32/64 for Intel and ARM. But as you know 64-bit ARM is not currently included in the downloads for Windows and Linux since Xojo is not yet ready for that.
Please try the new plugin part with 22.2pr4 and play with the classes. Please don’t hesitate to contact us with your questions or bug reports, so we can fix any issue before the final version ships later this month.
See also: Iterate with PCRE2