boost.regex正则表达式

通配符

一些简单的通配符:

+ :匹配一次或多次;
* :匹配0次或多次;
. 匹配除换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字 等价于 ‘[^A-Za-z0-9_]’。
\s 匹配任意的空白符
\d 匹配数字,需要转义\\d
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束

转义字符

cout << regex_match("123", regex("\d+")) << endl;     //结果为0,需要转义字符'\'
cout << regex_match("123", regex("\\d+")) << endl;    //结果为1,完全匹配

Boost C++ 的正则表达式库 Boost.Regex 可以应用正则表达式于 C++ 。 正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能。 虽然现在 C++ 仍然需要以 Boost C++ 库的形式提供这一功能,但是在将来正则表达式将进入 C++ 标准库。 Boost.Regex 库有望包括在下一版的 C++ 标准中。

Boost.Regex 库中两个最重要的类是 boost::regex 和 boost::smatch, 它们都在 boost/regex.hpp 文件中定义。 前者用于定义一个正则表达式,而后者可以保存搜索结果。

以下将要介绍 Boost.Regex 库中提供的三个搜索正则表达式的函数。

regex_match函数

函数 boost::regex_match() 用于字符串与正则表达式的比较。 在整个字符串匹配正则表达式时其返回值为 true 。

#include <boost/regex.hpp> 
#include <locale> 
#include <iostream> 

int main() 
{ 
  std::locale::global(std::locale("German")); 
  std::string s = "Boris Schäling"; 
  boost::regex expr("\\w+\\s\\w+"); 
  std::cout << boost::regex_match(s, expr) << std::endl; 
} 

regex_search函数

函数 boost::regex_search() 可用于在字符串中搜索正则表达式。

#include <boost/regex.hpp> 
#include <locale> 
#include <iostream> 

int main() 
{ 
  std::locale::global(std::locale("German")); 
  std::string s = "Boris Schäling"; 
  boost::regex expr("(\\w+)\\s(\\w+)"); 
  boost::smatch what; 
  if (boost::regex_search(s, what, expr)) 
  { 
    std::cout << what[0] << std::endl; 
    std::cout << what[1] << " " << what[2] << std::endl; 
  } 
} 

函数 boost::regex_search() 可以接受一个类型为 boost::smatch 的引用的参数用于储存结果。 函数 boost::regex_search() 只用于分类的搜索, 本例实际上返回了两个结果, 它们是基于正则表达式的分组。

存储结果的类 boost::smatch 事实上是持有类型为 boost::sub_match 的元素的容器, 可以通过与类 std::vector 相似的界面访问。 例如, 元素可以通过操作符 operator 访问。

另一方面,类 boost::sub_match 将迭代器保存在对应于正则表达式分组的位置。 因为它继承自类 std::pair ,迭代器引用的子串可以使用 first 和 second 访问。如果像上面的例子那样,只把子串写入标准输出流, 那么通过重载操作符 << 就可以直接做到这一点,那么并不需要访问迭代器。

请注意结果保存在迭代器中而 boost::sub_match 类并不复制它们, 这说明它们只是在被迭代器引用的相关字符串存在时才可以访问。

另外,还需要注意容器 boost::smatch 的第一个元素存储的引用是指向匹配正则表达式的整个字符串的,匹配第一组的第一个子串由索引 1 访问。

regex_replace函数

Boost.Regex 提供的第三个函数是 boost::regex_replace()。boost::regex_replace() 函数还需要一个格式参数,它决定了子串、匹配正则表达式的分组如何被替换。如果正则表达式不包含任何分组,相关子串将被用给定的格式一个个地被替换。这样下面程序输出的结果为 Boris_Schäling 。boost::regex_replace() 函数总是在整个字符串中搜索正则表达式,所以这个程序实际上将三处空格都替换为下划线。

#include <boost/regex.hpp> 
#include <locale> 
#include <iostream> 

int main() 
{ 
  std::locale::global(std::locale("German")); 
  std::string s = " Boris Schäling "; 
  boost::regex expr("\\s"); 
  std::string fmt("_"); 
  std::cout << boost::regex_replace(s, expr, fmt) << std::endl; 
} 


#include <boost/regex.hpp> 
#include <locale> 
#include <iostream> 

int main() 
{ 
  std::locale::global(std::locale("German")); 
  std::string s = "Boris Schäling"; 
  boost::regex expr("(\\w+)\\s(\\w+)"); 
  std::string fmt("\\2 \\1"); 
  std::cout << boost::regex_replace(s, expr, fmt) << std::endl; 
}

格式参数可以访问由正则表达式分组的子串,这个例子正是使用了这项技术,交换了姓、名的位置,于是结果显示为 Schäling Boris 。

示例程序

#include <boost/regex.hpp> 
#include <iostream> 
#include <string>
#include <cstdlib>
#include <stdlib.h>


boost::regex g_subexp("e[cl][oe][mc]");
boost::regex g_expr("^select ([a-zA-Z]*)\\sfrom\\s([a-zA-Z]*)\\ssse");

int Test()
{
	boost::cmatch what; 
    std::string content = "select name from table sse" ;
	boost::cmatch sub;

	if (boost::regex_match(content.c_str(), what, g_expr))
	{
		//regex_match :对整个输入块的匹配,整个块如不匹配则不能成功
       std::cout << "boost::cmatch size: " <<  what.size() << std::endl;
		for(unsigned int i = 0; i < what.size(); i++)
			std::cout << "str: " << what[i].str() << std::endl;
	}
	else
	{
		std::cout << "Error Match" << std::endl;
	}
	printf("%s\n",content.c_str());
	while (boost::regex_search(content.c_str(), sub, g_subexp))
	{
		// 单字搜索,将每次匹配到的结果输出
		printf("%s\n", sub.base());
		printf("%s\n", sub[0].str().c_str());
		content = sub[0].second;
        std::cout << "content: " << content << std::endl;
	}
	return 0 ;
}


int main(int argc, char *argv[]) 
{  
    {
        std::string s = "Boris Schaling"; 
        boost::regex expr("\\w+\\s\\w+"); 
        std::cout << boost::regex_match(s, expr) << std::endl; 
    }

    {
        std::string s = "hello world"; 
        // 在匹配规则中,以括号()的方式来划分组别,实例中的规则共有两个括号,所以共有两组数据
        boost::regex expr("(\\w+)\\s(\\w+)"); 
        boost::smatch what; 
        if (boost::regex_search(s, what, expr)) 
        { 
            std::cout << what[0] << std::endl; 
            std::cout << what[1] << " " << what[2] << std::endl; 
        } 
    } 

    {
        std::string s = " Boris Schaling "; 
        boost::regex expr("\\s"); 
        std::string fmt("_"); 
        std::cout << boost::regex_replace(s, expr, fmt) << std::endl; 
    }

    Test();

    return 0;
}
/*
output: 
1
hello world
hello world
_Boris_Schaling_
boost::cmatch size: 3
str: select name from table sse
str: name
str: table
select name from table sse
select name from table sse
elec
content: t name from table sse
*/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Erice_s

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值