|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionRegular 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 QuestionsQ: 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 Q: What Regular Expression Flavors are Supported?A: It depends on your implementation. Visual Studio 2008 supports these options: Q: What Header Do I Include?A: Q: What Namespace are Things In?A: This is the namespace for the Q: How Do I Do a Match?A: Construct a For example: std::string str = "Hello world";
std::tr1::regex rx("ello");
assert( regex_search(str.begin(), str.end(), rx) );
The function Q: How Do I Retrieve a Match?A: Use a form of For example, the following code searches for 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 Q: How Do I Do a Replace?A: Use The following code will replace “ 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 Q: How Do I Do a Global Replace?A: The function Q: How Do I Keep From Doing a Global Replace?A: Use the The fully qualified name for the flag is Q: How Do I Make a Regular Expression Case-insensitive?A: Use the The fully qualified name of the flag is History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||