Regex C++: 正则表达式(1)

自C++11起标准库提供了正则表达式库,允许我们使用通配符pattern查找和替换掉string中的字符.

Match: 将整个string拿来匹配某个regex.

Search: 查找某个string中与regex吻合的部分.

Replace: 将与正则表达式吻合的第一个(或者后续所有的)子序列替换掉.

Tokenize: 切分即通过指定来切分出来我们感兴趣的正则表达式匹配到的内容.

 

那么我们来看一个例子吧,先不用管正则表达式的文法:

#include <iostream>
#include <regex>
#include <string>
#include <utility>
int main()
{
	std::regex regOne("<.*>.*<.*>");
	std::string str("<tg>value</tag>");
	std::smatch resultOne;
	bool booleanOne = std::regex_match(str, resultOne, regOne);
	std::cout << std::boolalpha << booleanOne << std::endl;

	std::smatch resultTwo;
	bool booleanTwo = std::regex_search(str, resultTwo, regOne);
	std::cout << std::boolalpha << booleanTwo << std::endl;

	return 0;
}

std::regex_match()检查是否整个字符序列匹配某个正则表达式.

std::regex_search()检查是否部分字符序列匹配某个正则表达式.

 

std::basic_regex

template <typename charT, typename traits = regex_traits<charT> > 
class basic_regex;

其中我们上面使用的std::regex其实就是一个: typedef  std::basic_regex<char> regex;

此外官方还特例化了一个std::wregex其实是: typedef std::basic_regex<w_char> wregex;

我们在构造 std::basic_regex对象的时候除了给出必须给出的正则表达式外海可以指定要给flag:

flag是个可选的参数,默认值为 ECMAScript(使用的是ECMA-262规范,很多web浏览器用的也是这个标准.


//默认构造函数.
basic_regex();

//拷贝构造函数.
basic_regex (const basic_regex& rgx);

//移动构造函数.
basic_regex (basic_regex&& rgx) noexcept;


//显式的接受一个C风格字符串的构造函数.
explicit basic_regex ( const charT* str, flag_type flags = ECMAScript );


//接受一个C风格的字符串前len个字符作为参数构造一个std::basic_regex对象.
basic_regex ( const charT* str, size_t len, flag_type flags = ECMAScript );


//接受一个std::basic_string.
template <class ST, class SA>
  explicit basic_regex ( const basic_string<charT,ST,SA>& str, flag_type flags = ECMAScript );


//接受一对迭代器范围内的字符.
template <class ForwardIterator>
  basic_regex (ForwardIterator first, ForwardIterator last, flag_type flags = ECMAScript );


//接受一个{}括住的字符列表.
basic_regex (initializer_list<charT> il, flag_type flags = ECMAScript );

通过上面的构造函数(拷贝构造函数和移动构造函数除外)我们发现默认的flag都是ECMAScript.

所有的flag都定义在命名空间: std::regex_constants中.

以下均属于: syntax_option_type类型,其实也就是定义在std::regex_constants这个namespace中的enum类型.

std::regex_constants::icase :该flag支持在正则表达式匹配过程中忽略大小写.

std::regex_constants::nosubs :表flag表明在匹配过程中不保存匹配的子表达式.

std::regex_constants::optimize :表明正则表达式匹配时候执行速度优于构造速度.

std::regex_constants::ECMAScript : 使用ECMA-262指定的语法.

std::regex_constants::extended : 使用POSIX扩展的正则表达式语法.

std::regex_constants::basic : 使用POSIX的基本正则表达式语法.

std::regex_constants::awk : 使用POSIX版本的awk语言的语法.

std::regex_constants::grep : 使用POSIX版本的grep的语法.

std::regex_constants::egrep : 使用POSIX版本的egrep的语法.

std::basic_regex有几个成员函数让我们来看看:

std::basic_regex::assign


//等同于拷贝构造函数.
basic_regex& assign( const basic_regex& other );

//等同于移动构造函数.
basic_regex& assign( basic_regex&& that );

//接受一个C风格的字符串和flag用来构造一个std::basic_regex对象.	
basic_regex& assign( const CharT* s, flag_type f = std::regex_constants::ECMAScript );

//接受一个c风格的字符串的前count个字符用来构造一个std::basic_regex对象.
basic_regex& assign( const charT* ptr, size_t count, flag_type f = std::regex_constants::ECMAScript );

//接受一个std::basic_string和一个flag用来构造.	
template< class ST, class SA >
basic_regex& assign( const std::basic_string<CharT,ST,SA>& str, flag_type f = std::regex_constants::ECMAScript );

//接受一对 iterator范围内的字符用来构造.
template< class InputIt >
basic_regex& assign( InputIt first, InputIt last, flag_type f = std::regex_constants::ECMAScript );

//接受一个{}括着的字符列表用来构造.
template<typename charT>
basic_regex& assign( std::initializer_list<CharT> ilist, flag_type f = std::regex_constants::ECMAScript );

std::basic_regex::mark_count

unsigned mark_count() const;

返回正则表达式中子表达式的数目.

std::basic_regex::flags

flag_type flags() const;

返回正则表达式中的flags

Demo for std::basic_regex

#include <iostream>
#include <regex>
#include <string>
#include <utility>

int main()
{
	std::string str("<person>value</person>");
	std::basic_regex<char> regex("<(.*)>.*</.*>", std::regex_constants::icase | std::regex_constants::ECMAScript);

	std::cout << regex.mark_count() << std::endl;

	std::match_results<std::string::const_iterator> result;
	bool boolean = std::regex_search(str, result, regex);
	
	std::cout << std::boolalpha << "是否匹配成功: " << boolean << " " << regex.mark_count() << std::endl;
	std::cout << result[0].str() << std::endl;

	return 0;
}

 

 

 

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/726780

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值