Introduction to Compile-Time Regular Expressions (CTRE) v3
The Compile-Time Regular Expressions (CTRE) library offers a revolutionary approach to utilizing regular expressions by performing regex operations during compile-time in C++. Traditional regular expressions are typically matched and processed during program runtime, which can sometimes hinder performance. CTRE, however, takes advantage of compile-time capabilities, offering a faster and more efficient solution for regular expression matching, searching, and capturing.
Features of the Library
One of the standout features of the CTRE library is its capability to perform various regex operations both at compile-time and runtime:
- Matching: Determine if an input fully matches a regular expression.
- Searching: Check if a regex is found anywhere within the input.
- Capturing: Extract specific parts of the input string with support for named captures.
- Back-References: Support for back-references using the \g{N} or \1...\9 syntax.
- Multiline Support: Functions that handle multilined text with ease.
- Unicode Properties and UTF-8 Support: Work seamlessly with Unicode, allowing for broader text processing capabilities.
PCRE Syntax and Limitations
CTRE implements most of the Perl Compatible Regular Expressions (PCRE) syntax, which is widely known and used. However, there are a few exceptions. CTRE does not support callouts, comments, conditional patterns, control characters, match point resets, named characters, octal numbers, options/modes, subroutines, or Unicode grapheme clusters.
Basic API
CTRE provides a simple yet powerful API with functions for checking matches, searching, and starting matches. These functions can handle both whole input matching and searching within parts of an input, delivering results that can be easily deconstructed into structured bindings.
Here are some basic operations you can perform using the API:
- Match: Use
ctre::match<"regex">(subject)
for full-string matches. - Search: Use
ctre::search<"regex">(subject)
to find a regex anywhere in the input. - Starts With: Check if the input starts with a match using
ctre::starts_with<"regex">(subject)
. - Range and Tokenize: Utilize
ctre::range
andctre::tokenize
to obtain or split input based on regex patterns.
Functors and Supported Inputs
CTRE functions act as functors, allowing them to be used without parentheses. They support std::string
-like objects and pairs of forward iterators, making the library versatile and practical for various input types.
Unicode and Compiler Compatibility
The library supports Unicode, but requires specific inclusions to enable features, using headers like <ctre-unicode.hpp>
. It's compatible with several modern C++ compilers:
- Clang: Version 7.0+ for template UDL and C++17 syntax.
- GCC: Version 8.0+ with support for both C++17 and C++20 syntax.
- MSVC: Version 14.29+ (Visual Studio 16.11+) compliant with C++20.
Examples
CTRE is potent in practical applications, such as extracting numbers from a string, parsing dates, and acting as a lexer. Its capability to handle structured data and its capturing features make it a compelling choice for many text processing tasks.
Installation and Testing
CTRE can easily be installed using the vcpkg manager with the command ./vcpkg install ctre
. The project maintains up-to-date versions, ensuring that users always have access to the latest features and optimizations. For developers interested in contributing or testing, running make
in the project root is sufficient to initiate tests.
In summary, that the Compile-Time Regular Expressions library merges cutting-edge compile-time capabilities with practical regular expression utilities, offering unmatched performance and flexibility. It's especially suitable for developers seeking to optimize regex operations in their C++ applications.