Can I get the string with regular expression from std::regex
? Or should I save it somewhere else if I want to use it later?
In boost you can do this:
boost::regex reg("pattern");
string p = reg.str();
or use << operator
cout << reg;
will print pattern.
but in std::regex
there is no str() or operator<<. Should I save my string somewhere else or I just can't find it?
In debugger I can see what's in std::regex
.
Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .
C++ regex Example [a-zA-Z0-9]+”; The above regex can be interpreted as follows: Match a letter (lowercase and then uppercase) or an underscore. Then match zero or more characters, in which each may be a letter, or an underscore or a digit.
Regex examples. A simple example for a regular expression is a (literal) string. For example, the Hello World regex matches the "Hello World" string. .
A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.
I just looked in N3225, section 28.4 (header <regex>
synopsis) and indeed, the basic_regex
template has no member function str
, and there are no operator<<
provided.
The paragraph 28.8/2 provides a little insight on this :
Objects of type specialization of
basic_regex
are responsible for converting the sequence ofcharT
objects to an internal representation. It is not specified what form this representation takes, nor how it is accessed by algorithms that operate on regular expressions.
What I understand is that the standard mandates that basic_regex
can be constructed from const charT *
but does not require the implementation to keep this string.
The MSDN docs seem to show that there's no publicly accessible way to retrieve the regex pattern from a constructed object, so I would say that you need to save the string yourself.
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