c++ 11正则表达

本文档是C++ TR1正则表达式的快速入门指南,回答了如何使用TR1扩展进行匹配、替换等常见问题。提供Visual Studio 2008、Boost和Dinkumware等实现的支持情况。
摘要由CSDN通过智能技术生成

Quick Start for C++ TR1 Regular Expressions

John D. Cook26 Jun 2014
report
This article answers some of the first questions that come up when using regular expressions in C++ TR1

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 theBoost 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: basicextendedECMAScript,awkgrepegrep.

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 formatch_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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值