boost xpressive的用法1

    xpressive是强大的正则表达式库,使用非常方便。首先将下载的boost_1_54_0.zip解压,其中有boost文件夹,拷贝到vs2010的包含目录,例如“C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include”。这样就可以找到xpressive相关的头文件了。这就够了,xpressive不需要配置lib,因为实现都在 *.hpp 文件中包含了。

实例1:匹配整个字符串

#define _SCL_SECURE_NO_WARNINGS // 去除vs编译警告

#include <iostream>
#include <boost/xpressive/xpressive.hpp>

using namespace boost::xpressive;

int main()
{
	std::string hello( "hello world" );

	sregex rex = sregex::compile( "(\\w+) (\\w+)" );
	smatch what;

	if( regex_match( hello, what, rex ) )
	{
		std::cout << what[0] << '\n'; // whole match
		std::cout << what[1] << '\n'; // first capture
		std::cout << what[2] << '\n'; // second capture
	}

	return 0;
}

运行结果:

hello world
hello
world

这里的表达式是'(\w+) (\w+)' ,(在程序中用"\\"转义表示“\"),\w 表示字母或数字,+表示至少一个,所以该表达式表示“用空格分隔的两个字串”。

还有一个“表达式”的概念,整个匹配结果是第0个表达式,然后每个“()”对应一个子表达式。

'(\w+) (\w+)':what[0]

'(\w+)':what[1]

'(\w+)':what[2]

注意

sregex 的操作对象时string,例如 std::string sstr;cregex的操作对象时c风格的字符串, 例如 char cstr[]。分别对应的匹配结果使用smatch和cmatch。


实例2:匹配字串中的一部分,只查找第一个匹配的字串

匹配整个字串用regex_match,匹配字串中的一部分用regex_search。

#define _SCL_SECURE_NO_WARNINGS // 去除vs编译警告

#include <iostream>
#include <boost/xpressive/xpressive.hpp>

using namespace boost::xpressive;

int main()
{
	char const *str = "I was born on 5/30/1973 at 7am.";//c语言风格的字符串所以对应于cregex和cmatch

	//                                (month) (连字符) (day)   (year (?: 特殊))
	//                                (表达式1)(表达式2]  (3)    (4    (5 不保存))  
	cregex date = cregex::compile( "(\\d{1,2})([/-])(\\d{1,2})\\2((?:\\d{2}){1,2})" );
	//注意每个表达式的序号
	//注意 \2 的用法,在正则表达式的内部表示匹配的第2个子表达式
	//注意(?:x)的用法,表示不用把该括号中的内容作为一个字表达式保存到结果中

	cmatch what;

	if( regex_search( str, what, date ) )
	{
		assert(what.size() == 5); //从what[0] 到 what[4] size为5,因为(?:\d{2}没有保存到最后的结果中
		std::cout << what[0]     << '\n'; // whole match
		std::cout << what[3]   << '\n'; // the day
		std::cout << what[1] << '\n'; // the month
		std::cout << what[4] << '\n'; // the delimiter
	}
	return 0;
}

/*
5/30/1973
30
5
1973
*/

实例3:字符串的替换

#define _SCL_SECURE_NO_WARNINGS // 去除vs编译警告

#include <iostream>
#include <boost/xpressive/xpressive.hpp>

using namespace boost::xpressive;

int main()
{
	std::string str( "I was born on 5/30/1973 at 7am." );

	//这一次处理的对象时string,所以用sregex
	sregex date = sregex::compile( "(\\d{1,2})([/-])(\\d{1,2})\\2((?:\\d{2}){1,2})" );

	//  $&代表匹配成功的字串
	std::string format( "<date>$&</date>" );

	std::string strWhole = regex_replace( str, date, format );
	std::cout << strWhole << '\n';
	// result: I was born on <date>5/30/1973</date> at 7am.

	//  $&代表匹配成功的字串
	std::string format2( "<time string/>" );

	std::string strWhole2 = regex_replace( str, date, format2 );
	std::cout << strWhole2 << '\n';
	//result: I was born on <time string/> at 7am.

	//  $&代表匹配成功的字串
	std::string format3( "<year>$4</year>" );

	std::string strWhole3 = regex_replace( str, date, format3 );
	std::cout << strWhole3 << '\n';
	//result: I was born on <year>1973</year>  at 7am.

	return 0;
}

实例4: 一个字符串中有多个字串匹配成功,迭代显示每一个成功的匹配

#define _SCL_SECURE_NO_WARNINGS // 去除vs编译警告

#include <iostream>
#include <string>
#include <boost/xpressive/xpressive.hpp>

using namespace boost::xpressive;

int main()
{
	std::string str( "This is his face." );

	sregex token = sregex::compile("\\w+");

	sregex_iterator cur( str.begin(), str.end(), token );
	sregex_iterator end;

	for( ; cur != end; ++cur )
	{
		smatch const &what = *cur;
		assert(what.size() == 1);
		std::cout << what[0] << '\n';
	}

	return 0;
}
/* 运行结果
This
is
his
face
*/

需要注意这里的sregex_iterator其实就是sregex的集合,而且sregex_iterator的默认构造函数(例如本例中的end变量)相当于一个“截止”符。



你可能感兴趣的:(正则表达式,VS2010,boost)