Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions in c++ STL

Tags:

c++

regex

stl

Is there any native library in STL which is tested and works without any extra compiler options? I tried to use <regex>, but the compiler outputs this:

In file included from /usr/include/c++/4.3/regex:40, from main.cpp:5: /usr/include/c++/4.3/c++0x_warning.h:36:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

like image 938
Radek Simko Avatar asked Jan 17 '11 17:01

Radek Simko


People also ask

Is STL a regex?

<regex> library in C++ STL These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters. These functions are used to apply the regular expression encapsulated in a regex to a target sequence of characters.

Can you use regex in C?

It is used in every programming language like C++, Java, and Python. Used to find any of the characters or numbers specified between the brackets. Used to find any digit.

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What is regular expression expression with example?

A simple example for a regular expression is a (literal) string. For example, the Hello World regex matches the "Hello World" string. . (dot) is another example for a regular expression. A dot matches any single character; it would match, for example, "a" or "1".


1 Answers

G++ 4.3 (and presumably later versions as well) is just being careful about the header files for maximum standards conformance.

If you're programming in C++98 (the current standard that's been around for a while), then regular expression support was added in tech report 1, and the and the header files are in a special tr1 directory, and the contents are in a special namespace std::tr1.

In the new C++0x standard, the regular expression support has been merged into the standard library, so it can be found in the header regex and namespace std.

G++ makes sure that you use the right version for the --std= version you specified on the command line, even though internally they're both the same implementation.

So to make regex work without switching to --std=c++0x, just

#include <tr1/regex>
like image 193
Ken Bloom Avatar answered Sep 21 '22 15:09

Ken Bloom