Boost汉字匹配 -- 宽字符
原文链接:http://blog.csdn.net/sptoor/article/details/4930069
思路:汉字匹配,把字符都转换成宽字符,然后再匹配。
- 需要用到以下和宽字符有关的类:
1、wstring:
作为STL中和string相对应的类,专门用于处理宽字符串。方法和string都一样,区别是value_type是wchar_t。wstring类的对象要赋值或连接的常量字符串必须以L开头标示为宽字符。
2、wregex:
和regex相对应,专门处理宽字符的正则表达式类。同样可以使用regex_match()和regex_replace()等函数。regex_match()的结果需要放在wsmatch类的对象中。
- 字符和宽字符的相互转换:
1、RTL的方法
//把字符串转换成宽字符串
setlocale( LC_CTYPE, "" ); // 很重要,没有这一句,转换会失败。
int iWLen= mbstowcs( NULL, sToMatch.c_str(), sToMatch.length() ); // 计算转换后宽字符串的长度。(不包含字符串结束符)
wchar_t *lpwsz= new wchar_t[iWLen+];
int i= mbstowcs( lpwsz, sToMatch.c_str(), sToMatch.length() ); // 转换。(转换后的字符串有结束符)
wstring wsToMatch(lpwsz);
delete []lpwsz; //把宽字符串转换成字符串,输出使用
int iLen= wcstombs( NULL, wsm[].str().c_str(), ); // 计算转换后字符串的长度。(不包含字符串结束符)
char *lpsz= new char[iLen+];
int i= wcstombs( lpsz, wsm[].str().c_str(), iLen ); // 转换。(没有结束符)
lpsz[iLen] = '\0';
string sToMatch(lpsz);
delete []lpsz;
2、Win32 SDK的方法
//把字符串转换成宽字符串
int iWLen= MultiByteToWideChar( CP_ACP, , sToMatch.c_str(), sToMatch.size(), , ); // 计算转换后宽字符串的长度。(不包含字符串结束符)
wchar_t *lpwsz= new wchar_t [iWLen+];
MultiByteToWideChar( CP_ACP, , sToMatch.c_str(), sToMatch.size(), lpwsz, iWLen ); // 正式转换。
wsz[iWLen] = L'\0';
//把宽字符串转换成字符串,输出使用
int iLen= WideCharToMultiByte( CP_ACP, NULL, wsResult.c_str(), -, NULL, , NULL, FALSE ); // 计算转换后字符串的长度。(包含字符串结束符)
char *lpsz= new char[iLen];
WideCharToMultiByte( CP_OEMCP, NULL, wsResult.c_str(), -, lpsz, iLen, NULL, FALSE); // 正式转换。
Result.assign( lpsz, iLen- ); // 对string对象进行赋值。
示例:
通过以下程序我们可以看到,对字符串做\w匹配时,某些字会引起匹配失败。通过把字符串转换成宽字符串尝试解决这个问题。
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
using std::wstring;
#include <locale> #include "boost\tr1\regex.hpp"
using namespace boost; void MatchWords(string sToMatch)
{
regex rg("(\\w*)");
smatch sm;
regex_match( sToMatch, sm, rg );
cout << "匹配结果:" << sm[].str() << endl;
} void MatchWords(wstring wsToMatch)
{
wregex wrg(L"(\\w*)");
wsmatch wsm;
regex_match( wsToMatch, wsm, wrg ); int iLen= wcstombs( NULL, wsm[].str().c_str(), );
char *lpsz= new char[iLen+];
int i= wcstombs( lpsz, wsm[].str().c_str(), iLen );
lpsz[iLen] = '\0'; string sToMatch(lpsz);
delete []lpsz;
cout << "匹配结果:" << sToMatch << endl;
} void main()
{
string sToMatch("数超限");
MatchWords( sToMatch );
sToMatch = "节点数目超限";
MatchWords( sToMatch ); setlocale( LC_CTYPE, "" );
int iWLen= mbstowcs( NULL, sToMatch.c_str(), sToMatch.length() );
wchar_t *lpwsz= new wchar_t[iWLen+];
int i= mbstowcs( lpwsz, sToMatch.c_str(), sToMatch.length() ); wstring wsToMatch(lpwsz);
delete []lpwsz;
MatchWords( wsToMatch );
}
编译执行程序后输出:
匹配结果:数超限
匹配结果:
匹配结果:节点数目超限
第一行显示“数超限”匹配成功。但第二行“节点数超限”没有匹配到任何字符。只有转换成宽字符串之后才能够对“节点数超限”成功进行\w匹配。
声明:本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sptoor/article/details/4930069
Boost汉字匹配 -- 宽字符的更多相关文章
- c++ boost 汉字和模式串混用的例子
*=============================================================== * Copyright (C) All rights reserved ...
- SQL注入之Sqli-labs系列第三十二关(基于宽字符逃逸注入)
开始挑战第三十二关(Bypass addslashes) 0x1查看源代码 (1)代码关键点 很明显,代码中利用正则匹配将 [ /,'," ]这些三个符号都过滤掉了 function che ...
- 宽字符,Ansic和Unicode
电脑发展的初期,只是在美国等英文国家使用,英文只有26个字母和其它字符,一个字节最多可以表示256个字符,如字母"A"用0x41(二进制01000001)表示,字母"a& ...
- js字符串长度计算(一个汉字==两个字符)和字符串截取
js字符串长度计算(一个汉字==两个字符)和字符串截取 String.prototype.realLength = function() { return this.replace(/[^\x00-\ ...
- C#正则表达式匹配任意字符
原文:C#正则表达式匹配任意字符 不得不说正则很强大,尤其在字符串搜索上 匹配任意字符,包括汉字,换行符: [\s\S]*. 版权声明:本文为博主原创文章,未经博主允许不得转载.
- gcc编译器对宽字符的识别
最早是使用VC++工具来学习C++,学的越多就越对VC挡住的我看不见的东西好奇,总想多接触一些开发环境,今日抽空摸索了一下CodeBlocks这个开源的IDE使用方法,配置的编译器是MinGW的gcc ...
- [c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset
一.memset #include<stdio.h> #include<stdlib.h> #include<memory.h> void *mymemset(vo ...
- 彻底解密C++宽字符(二)
彻底解密C++宽字符(二) 转:http://club.topsage.com/thread-2227977-1-1.html 4.利用codecvt和use_facet转换 locale和facet ...
- 彻底解密C++宽字符(一)
彻底解密C++宽字符(一) 转:http://club.topsage.com/thread-2227977-1-1.html 1.从char到wchar_t “这个问题比你想象中复杂” 从字符到整数 ...
随机推荐
- Mac最新系统bssdb BUG
这个bug在Mac OS更新到10.14时候出现,当前系统版本 ➜ git:(master) sw_vers ProductName: Mac OS X ProductVersion: 10.14 B ...
- Perl6多线程1 Thread : new / run
先看一个小例子: ) { #默认参数 say $name; } sub B(:name($name)) { #默认参数为 any say $name; } A(); A(); B(); B(name ...
- caffe Python API 之Inference
#以SSD的检测测试为例 def detetion(image_dir,weight,deploy,resolution=300): caffe.set_mode_gpu() net = caffe. ...
- 出现ERROR: While executing gem ... (Gem::FilePermissionError)这种错误的解决办法
重新安装ruby即可解决 brew install ruby
- Python中的raw_input()和input()
raw_input()和input()都是python中的内建函数,用于读取控制台用户的输入,但有所区别: [nr@localhost conf]$ python Python 2.7.5 (defa ...
- 理解一条语句:SELECT difference(sum("value")) FROM "mq_enqueue" WHERE "channel" =~ /ActiveMQ_TEST/ AND $timeFilter GROUP BY time($interval)
最近使用grafana在查询InfluxDB中,用到了这一条语句 SELECT difference(sum("value")) FROM "mq_enqueue&quo ...
- Linux软件管理器(如何使用软件管理器来管理软件)
我们的Linux系统大部分都是某个Linux厂商的系统,所以这些厂商可以编译好一些软件来提供用户下载,用户下载完了之后就可以直接安装,从而省去了编译源码及其过程中的各种问题.这时我们就可以使用相应的软 ...
- C语言 五子棋2
#include<windows.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> # ...
- [你必须知道的.NET]第十九回:对象创建始末(下)
本文将介绍以下内容: 对象的创建过程 内存分配分析 内存布局研究 接上回[第十八回:对象创建始末(上)],继续对对象创建话题的讨论>>> 2.2 托管堆的内存分配机制 引用类型的实例 ...
- 5 Linux网络编程基础API
5.1 socket地址API 大端字节序(网络序):高位在低址,低位在高址 小端字节序(主机序):低位在低址,高位在高址 判断,利用联合的特性: #include <iostream> ...