原文链接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汉字匹配 -- 宽字符的更多相关文章

  1. c++ boost 汉字和模式串混用的例子

    *=============================================================== * Copyright (C) All rights reserved ...

  2. SQL注入之Sqli-labs系列第三十二关(基于宽字符逃逸注入)

    开始挑战第三十二关(Bypass addslashes) 0x1查看源代码 (1)代码关键点 很明显,代码中利用正则匹配将 [ /,'," ]这些三个符号都过滤掉了 function che ...

  3. 宽字符,Ansic和Unicode

    电脑发展的初期,只是在美国等英文国家使用,英文只有26个字母和其它字符,一个字节最多可以表示256个字符,如字母"A"用0x41(二进制01000001)表示,字母"a& ...

  4. js字符串长度计算(一个汉字==两个字符)和字符串截取

    js字符串长度计算(一个汉字==两个字符)和字符串截取 String.prototype.realLength = function() { return this.replace(/[^\x00-\ ...

  5. C#正则表达式匹配任意字符

    原文:C#正则表达式匹配任意字符 不得不说正则很强大,尤其在字符串搜索上 匹配任意字符,包括汉字,换行符: [\s\S]*. 版权声明:本文为博主原创文章,未经博主允许不得转载.

  6. gcc编译器对宽字符的识别

    最早是使用VC++工具来学习C++,学的越多就越对VC挡住的我看不见的东西好奇,总想多接触一些开发环境,今日抽空摸索了一下CodeBlocks这个开源的IDE使用方法,配置的编译器是MinGW的gcc ...

  7. [c/c++] programming之路(25)、字符串(六)——memset,Unicode及宽字符,strset

    一.memset #include<stdio.h> #include<stdlib.h> #include<memory.h> void *mymemset(vo ...

  8. 彻底解密C++宽字符(二)

    彻底解密C++宽字符(二) 转:http://club.topsage.com/thread-2227977-1-1.html 4.利用codecvt和use_facet转换 locale和facet ...

  9. 彻底解密C++宽字符(一)

    彻底解密C++宽字符(一) 转:http://club.topsage.com/thread-2227977-1-1.html 1.从char到wchar_t “这个问题比你想象中复杂” 从字符到整数 ...

随机推荐

  1. css 格式中id与class共存

    PHP文件中有一段:<div class="post-alt blog" id="post-alt"> CSS文件中有一段:.post-alt {X ...

  2. usb_submit_urb 解释的够够的

    /** * usb_submit_urb - issue an asynchronous transfer request for an endpoint * @urb: pointer to the ...

  3. Java Web 远程调试

    Java Web 远程 调试 Tomcat 下载压缩版服务器 环境:Tomcat.Eclipse,做远程调试我们并不需要其他特殊插件 1.配置Tomcat/bin/startup.bat 在前面增加代 ...

  4. 中高级JAVA面试知识点(个人整理)

    JVM运行时数据区域 方法区: 用 于存储虚拟机加载的类信息,常量,静态变量,JIT编译后的代码,所有线程共享 堆:所有线程共享,用来存储实例对象. 虚拟机栈:线程私有,生命周期与线程相同,每个方法被 ...

  5. curl 发送请求的时候报错

    AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see ...

  6. spring(四)之基于注解(Annotation-based)的配置.md

    注解 这里讲的注解有下面几个 @Autowired @Qualifier(" ") @Genre(" ") @Offline @Resource(name=&q ...

  7. acm专题---键树

    题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...

  8. LightOJ 1369 Answering Queries(找规律)

    题目链接:https://vjudge.net/contest/28079#problem/P 题目大意:给你数组A[]以及如下所示的函数f: long long f( int A[], int n  ...

  9. springcloud 显示服务详细健康信息

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  10. Python全栈开发之1、输入输出与流程控制

    Python简介 python是吉多·范罗苏姆发明的一种面向对象的脚本语言,可能有些人不知道面向对象和脚本具体是什么意思,但是对于一个初学者来说,现在并不需要明白.大家都知道,当下全栈工程师的概念很火 ...