In JS regular expressions symbols ^
and $
designate start and end of the string. And only with /m
modifier (multiline mode) they match start and end of line - position before and after CR/LF.
But in std::regex/ECMAscript mode symbols ^
and $
match start and end of line always.
Is there any way in std::regex to define start and end of the string match points? In other words: to support JavaScript multiline mode ...
By default, ECMAscript mode already treats ^
as both beginning-of-input and beginning-of-line, and $
as both end-of-input and end-of-line. There is no way to make them match only beginning or end-of-input, but it is possible to make them match only beginning or end-of-line:
When invoking std::regex_match
, std::regex_search
, or std::regex_replace
, there is an argument of type std::regex_constants::match_flag_type
that defaults to std::regex_constants::match_default
.
^
matches only beginning-of-line, specify std::regex_constants::match_not_bol
$
matches only end-of-line, specify std::regex_constants::match_not_eol
std::regex_constants::match_not_bol | std::regex_constants::match_not_eol
)^
and regardless of the presence of std::regex_constants::match_not_bol
by specifying std::regex_constants::match_continuous
This is explained well in the ECMAScript grammar documentation on cppreference.com, which I highly recommend over cplusplus.com in general.
Caveat: I've tested with MSVC, Clang + libc++, and Clang + libstdc++, and only MSVC has the correct behavior at present.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With