Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get string with pattern from std::regex in VC++ 2010

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.

like image 773
Mariusz Pawelski Avatar asked Dec 21 '10 15:12

Mariusz Pawelski


People also ask

How do I match a pattern in 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 "@" .

How do you write regex expressions in C++?

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.

Can regex be a string?

Regex examples. A simple example for a regular expression is a (literal) string. For example, the Hello World regex matches the "Hello World" string. .

Is regex a pattern?

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.


2 Answers

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 of charT 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.

like image 110
icecrime Avatar answered Oct 07 '22 09:10

icecrime


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.

like image 34
Jon Avatar answered Oct 07 '22 11:10

Jon