Click here to Skip to main content
Click here to Skip to main content

Quick Start for C++ TR1 Regular Expressions

By , 23 May 2008
 

Introduction

Regular expression syntax is fairly similar across many environments. However, the way you use regular expressions varies greatly. For example, once you've crafted your regular expression, how do you use it to find a match or replace text? It's easy to find detailed API documentation, once you know what API to look up. Figuring out where to start is often the hardest part.

This article assumes you're familiar with regular expressions and want to work with regular expressions in C++ using the Technical Report 1 (TR1) proposed extensions to the C++ Standard Library. It's a quick start guide, briefly answering some of the first questions you're likely to ask. For more details, see Getting started with C++ TR1 regular expressions or dive into the documentation that comes with your implementation.

Quick Start Questions

Q: Where Can I Get TR1?

A: Support for TR1 extensions in Visual Studio 2008 is added as a feature pack. Other implementations include the Boost and Dinkumware. The GNU compiler gcc added support for TR1 regular expressions in version 4.3.0.

Q: What Regular Expression Flavors are Supported?

A: It depends on your implementation. Visual Studio 2008 supports these options: basic, extended, ECMAScript, awk, grep, egrep.

Q: What Header Do I Include?

A: <regex>

Q: What Namespace are Things In?

A: std::tr1

This is the namespace for the regex class and functions such as regex_search. Flags are contained in the nested namespace std::tr1::regex_constants.

Q: How Do I Do a Match?

A: Construct a regex object and pass it to regex_search.

For example:

std::string str = "Hello world";
std::tr1::regex rx("ello");
assert( regex_search(str.begin(), str.end(), rx) );

The function regex_search returns true because str contains the pattern ello. Note that regex_match would return false in the example above because it tests whether the entire string matches the regular expression. regex_search behaves more like most people expect when testing for a match.

Q: How Do I Retrieve a Match?

A: Use a form of regex_search that takes a match_result object as a parameter.

For example, the following code searches for <h> tags and prints the level and tag contents.

std::tr1::cmatch res;
str = "<h2>Egg prices</h2>";
std::tr1::regex rx("<h(.)>([^<]+)");
std::tr1::regex_search(str.c_str(), res, rx);
std::cout << res[1] << ". " << res[2] << "\n";

This code would print 2. Egg prices. The example uses cmatch, a typedef provided by the library for match_results<const char* cmatch>.

Q: How Do I Do a Replace?

A: Use regex_replace.

The following code will replace “world” in the string “Hello world” with “planet”. The string str2 will contain “Hello planet” and the string str will remain unchanged.

std::string str = "Hello world";
std::tr1::regex rx("world");
std::string replacement = "planet";
std::string str2 = std::tr1::regex_replace(str, rx, replacement);

Note that regex_replace does not change its arguments, unlike the Perl command s/world/planet/. Note also that the third argument to regex_replace must be a string class and not a string literal.

Q: How Do I Do a Global Replace?

A: The function regex_replace does global replacements by default.

Q: How Do I Keep From Doing a Global Replace?

A: Use the format_first_only flag with regex_replace.

The fully qualified name for the flag is std::tr1::regex_constants::format_first_only and would be the fourth argument to regex_replace.

Q: How Do I Make a Regular Expression Case-insensitive?

A: Use the icase flag as a parameter to the regex constructor.

The fully qualified name of the flag is std::tr1::regex_constants::icase.

History

  • 22nd May, 2008: Initial post
  • 23rd May, 2008: Added examples

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

John D. Cook
United States United States
Member
I am an independent consultant in software development and applied mathematics. I help companies learn from their data to make better decisions.
 
Check out my blog or send me a note.
 

 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalregex_replacememberpribylf10 Apr '12 - 19:11 
Hi, I need advice. I have simple artifical example.
 
#include <regex>
const std::string sStdPattern = "(A)(B)(B)(B)(B)(B)(B)(B)(B)(B)(B)(B)(B)(B)(B)";
const std::string sStdReplacement = "$11";
const std::string sStdText = "ABBBBBBBBBBBBBB";
const std::tr1::regex rx(sStdPattern);
const std::string sStdResult = std::tr1::regex_replace(sStdText, rx, sStdReplacement);
 
I need output "A1" - Letter "A" from first group (A) and number one "1". But code returns 11th group (B) = so output is "B".
 
Any suggestions?
 
Thx. Frank
GeneralMy vote of 3membergomons10 Mar '12 - 21:33 
Only basic examples. There isn't iterator examples.
GeneralFind position of sub string inside a given string. /os: VS/vc++(2005) win/xpmembermeirab30 Jan '09 - 12:30 
In: http://www.boost.org/doc/libs/1_37_0/doc/html/string_algo/usage.html#id3676944
 
there is an example: Find Iterator
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
string str1("abc-*-ABC-*-aBc");
// Find all 'abc' substrings (ignoring the case)
// Create a find_iterator
typedef find_iterator<string::iterator> string_find_iterator;
for(string_find_iterator It=
make_find_iterator(str1, first_finder("abc", is_iequal()));
It!=string_find_iterator();
++It)
{
cout << copy_range<std::string>(*It) << endl;
}
 
// Output will be:
// abc
// ABC
// aBC
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 
i need help in finding the position of substring inside a given string.
 
the demo program uses www.boost.org string routine.
(so it must be installed in order to find the place where sub programs store the position)
 
i need the (begin end) position at every iteration of the loop.
 
thank you.
 

here is the program:
 
#include "stdafx.h"
 
#include <fstream>
#include <iostream>
#include <iterator>
 
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
 
void count_ss(std::string& s )
{
//This sample code searches for every instance of the string "<bbb" in a given string
//and counts the total number of instances:
 
int i = 0;
int ss_appearances = 0;
int ss_end_appearances = 0;
 
// Find all '<bbb' substrings (ignoring the case)
// Create a find_iterator
typedef find_iterator<string::iterator> string_find_iterator;
for (string_find_iterator It =
make_find_iterator(s, first_finder("<bbb", is_iequal()));
It != string_find_iterator();
++It )
{
cout << copy_range <std::string>(*It) << endl;
ss_appearances++;
}
cout << ss_appearances << endl;
}
 

int main(int argc, char** argv)
{
string str1("<bbb-abc-<bbb-*-<bbb-ABC-*<bbb-aBc");
count_ss(str1 );
 
return 0;
}
GeneralAdditional infomemberSergey Sotnikov18 Nov '08 - 21:22 
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339[^]
http://msdn.microsoft.com/en-us/library/bb982727.aspx[^]
Questionhow to set singleline option ?memberflyingxu15 Nov '08 - 17:36 
in c#, it's RegexOptions.SingleLine
in boost, it's match_not_dot_newline,
what's it in vc2008 tr1?
GeneralYour blog article is quite goodmemberCodeWizard195123 May '08 - 7:14 
I think the actual work presentation in your blog link is most excellent. You should consider submitting your post as an article. If your blog site goes down for some reason, your work will still be publicly available.
 
I've enjoyed some of your past articles. I only wish you had a better venue to present your work. Keep it up. I work for an R & D organization myself, and your articles are far more pertinent to my work than most of the other code on here.
 
Keep up the good work!
 
CodeWiz51
 
-- Life is not a spectator sport. I came to play.
Code's Musings | Code's Articles

GeneralMore infoadminChris Maunder22 May '08 - 17:15 
Sample code and more in depth information is needed here
 
cheers,
Chris Maunder
CodeProject.com : C++ MVP

GeneralRe: More infomemberJohn D. Cook23 May '08 - 0:13 
My intention was to keep this article very brief, something quick to read that gets you started. The first link in the article points to a document with more sample code and more details.
GeneralRe: More infomemberMaximilien23 May '08 - 4:30 
the document you link to should have been the article itself.
 
(thanks btw)
 
Maximilien Lincourt
Your Head A Splode - Strong Bad

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 23 May 2008
Article Copyright 2008 by John D. Cook
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid