在 C++ 中编译时间正则表达式
Fast compile-time regular expressions with support for matching/searching/capturing during compile-time or runtime.
You can use the single header version from directory single-header. This header can be regenerated with make single-header. If you are using cmake, you can add this directory as subdirectory and link to target ctre.
More info at compile-time.re
ctre::match<"REGEX">(subject); // C++20
"REGEX"_ctre.match(subject); // C++17 + N3599 extension
search or starts_with)(?<name>...))\g{N} syntax, and \1...\9 syntax too)multiline_) functionsThe library is implementing most of the PCRE syntax with a few exceptions:
\cX)\K)\X)More documentation on pcre.org.
Not all escaped characters are automatically inserted as self, behaviour of the library is escaped characters are with special meaning, unknown escaped character is a syntax error.
Explicitly allowed character escapes which insert only the character are:
\-\"\<\>
This is approximated API specification from a user perspective (omitting constexpr and noexcept which are everywhere, and using C++20 syntax even the API is C++17 compatible):
…
…
All the functions (ctre::match, ctre::search, ctre::starts_with, ctre::range, ctre::tokenize, ctre::split) are functors and can be used without parenthesis:
auto matcher = ctre::match<"regex">;
if (matcher(input)) ...
std::string-like objects (std::string_view or your own string if it's providing begin/end functions with forward iterators)To enable you need to include:
<ctre-unicode.hpp><ctre.hpp> and <unicode-db.hpp>Otherwise you will get missing symbols if you try to use the unicode support without enabling it.
The compiler must support extension N3599, for example as GNU extension in gcc (not in GCC 9.1+) and clang.
constexpr auto match(std::string_view sv) noexcept {
using namespace ctre::literals;
return "h.*"_ctre.match(sv);
}
If you need extension N3599 in GCC 9.1+, you can't use -pedantic. Also, you need to define macro CTRE_ENABLE_LITERALS.
You can provide a pattern as a constexpr ctll::fixed_string variable.
static constexpr auto pattern = ctll::fixed_string{ "h.*" };
constexpr auto match(std::string_view sv) noexcept {
return ctre::match<pattern>(sv);
}
(this is tested in MSVC 15.8.8)
Currently, the only compiler which supports cNTTP syntax ctre::match<PATTERN>(subject) is GCC 9+.
constexpr auto match(std::string_view sv) noexcept {
return ctre::match<"h.*">(sv);
}
std::optional<std::string_view> extract_number(std::string_view s) noexcept {
if (auto m = ctre::match<"[a-z]+([0-9]+)">(s)) {
return m.get<1>().to_view();
} else {
return std::nullopt;
}
}
…
auto result = ctre::match<"(?<year>\\d{4})/(?<month>\\d{1,2})/(?<day>\\d{1,2})">(s);
return date{result.get<"year">(), result.get<"month">, result.get<"day">};
// or in C++ emulation, but the object must have a linkage
static constexpr ctll::fixed_string year = "year";
static constexpr ctll::fixed_string month = "month";
static constexpr ctll::fixed_string day = "day";
return date{result.get<year>(), result.get<month>(), result.get<day>()};
// or use numbered access
// capture 0 is the whole match
return date{result.get<1>(), result.get<2>(), result.get<3>()};
enum class type {
unknown, identifier, number
};
struct lex_item {
type t;
std::string_view c;
};
std::optional<lex_item> lexer(std::string_view v) noexcept {
if (auto [m,id,num] = ctre::match<"([a-z]+)|([0-9]+)">(v); m) {
if (id) {
return lex_item{type::identifier, id};
} else if (num) {
return lex_item{type::number, num};
}
}
return std::nullopt;
}
This support is preliminary, probably the API will be changed.
auto input = "123,456,768"sv;
for (auto match: ctre::search_all<"([0-9]+),?">(input))
std::cout << std::string_view{match.get<0>()} << "\n";
#include <ctre-unicode.hpp>
#include <iostream>
// needed if you want to output to the terminal
std::string_view cast_from_unicode(std::u8string_view input) noexcept {
return std::string_view(reinterpret_cast<const char *>(input.data()), input.size());
}
int main() {
using namespace std::literals;
std::u8string_view original = u8"Tu es un génie"sv;
for (auto match: ctre::search_all<"\\p{Letter}+">(original))
std::cout << cast_from_unicode(match) << std::endl;
return 0;
}
You can download and install ctre using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install ctre
The ctre port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
Just run make in root of this project.
暂无开放 Issues,或尚未同步最近议题。