[\\u4e00-\\u9fa5]正则表达式不能匹配全部中文?

时间:2022-06-29 21:15:54
今天做个C++的小程序,出现了[\\u4e00-\\u9fa5]正则表达式不能匹配全部中文的情况,具体如下:
1、环境:
    操作系统--Xp虚拟机
    开发平台--VC6
    正则类库--boost1.34.0--regex

2、代码:

std::string str("是");

regex rx("[\\u4e00-\\u9fa5]+");

bool bl = regex_match(str , rx);
if (bl)
cout << "sucess" << endl;
else
cout << "unsucess" << endl;

std::string ph("加");
regex rxf("[\\u4e00-\\u9fa5]+");
bl = regex_match(ph,rx);
if(bl)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;

return 0;



3、输出是:
sucess
no

4、测试情况
“是”、“人”、“在”等中文字符能匹配,而“加”、“解”等则不能匹配。
5、问题:
  [\\u4e00-\\u9fa5]+是不是不能包含所有中文字符?如何才能匹配所有中文字符?

12 个解决方案

#1


[\\u4e00-\\u9fa5] 这个是Unicode set utf-16的编码范围。

std::string str("是"); 窄字符串,肯定不是utf16的。用wstring试试 str::wstring str(L"是");

#2


引用 1 楼 luciferisnotsatan 的回复:
[\\u4e00-\\u9fa5] 这个是Unicode set utf-16的编码范围。

std::string str("是"); 窄字符串,肯定不是utf16的。用wstring试试 str::wstring str(L"是");


regex_match不认wstring:
error C2665: 'regex_match' : none of the 32 overloads can convert parameter 1 from type 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >'
执行 cl.exe 时出错.

#3


也用宽字符版本的

template <class charT, class traits = regex_traits<charT>, class Allocator = std::allocator<charT>  >

class basic_regex;

typedef basic_regex<char> regex;

typedef basic_regex<wchar_t> wregex;

#4


再正常不过的现象。
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
extern "C" HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void ShowTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = TRUE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;
    wchar_t wc[2];

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48,0,0,0,0,0,0,0,GB2312_CHARSET ,0,0,0,0,"宋体-方正超大字符集");
    SelectObject(hdc,hfont);
    wc[0]=0xD854u;
    wc[1]=0xDC00u;
    TextOutW(hdc,10,10,wc,2);
    DeleteObject(hfont);
    ReleaseDC(hwnd,hdc);
    getch();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}
#if 0
代理项或代理项对是一对共同表示单个字符的 16 位 Unicode 编码值。需要记住的关键一点是:
代理项对实际上是 32 位单个字符,不能再假定一个 16 位 Unicode 编码值正好映射到一个字符。

使用代理项对
代理项对的第一个值是高代理项,包含介于 U+D800 到 U+DBFF 范围内的 16 位代码值。
该对的第二个值是低代理项,包含介于 U+DC00 到 U+DFFF 范围内的值。通过使用代理项对,
16 位 Unicode 编码系统可以对已由 Unicode 标准定义的一百多万个其他字符 (220) 进行寻址。

在传递给 XmlTextWriter 方法的任何字符串中都可以使用代理项字符。不过,代理项字符在编写的
XML 中应该有效。例如,万维网联合会 (W3C) 建议不允许在元素或属性的名称中使用代理项字符。
如果字符串包含无效的代理项对,则引发异常。

另外,可以使用 WriteSurrogateCharEntity 写出与代理项对相对应的字符实体。字符实体以十六
进制格式写出,并用以下公式生成:

(highChar -0xD800) * 0x400 + (lowChar -0xDC00) + 0x10000

如果字符串包含无效的代理项对,则引发异常。下面的示例显示将代理项对作为输入的 WriteSurrogateCharEntity 方法。

C#复制
 // The following line writes &#x10000.
WriteSurrogateCharEntity ('\uDC00', '\uD800');
下面的示例生成一个代理项对文件,将其加载到 XmlReader 中,并用新的文件名保存文件。
然后,原始文件和新文件被加载回应用程序的 XML 文档对象模型 (DOM) 结构中以进行比较。

C#复制
 char lowChar, highChar;
char [] charArray = new char[10];
FileStream targetFile = new FileStream("SurrogatePair.xml",
      FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

lowChar = Convert.ToChar(0xDC00);
highChar = Convert.ToChar(0xD800);
XmlTextWriter tw = new XmlTextWriter(targetFile, null);
tw.Formatting = Formatting.Indented;
tw.WriteStartElement("root");
tw.WriteStartAttribute("test", null);
tw.WriteSurrogateCharEntity(lowChar, highChar);
lowChar = Convert.ToChar(0xDC01);
highChar = Convert.ToChar(0xD801);
tw.WriteSurrogateCharEntity(lowChar, highChar);
lowChar = Convert.ToChar(0xDFFF);
highChar = Convert.ToChar(0xDBFF);
tw.WriteSurrogateCharEntity(lowChar, highChar);

// Add 10 random surrogate pairs.
// As Unicode, the high bytes are in lower
// memory; for example, word 6A21 as 21 6A.
// The high or low is in the logical sense.
Random random = new Random();
for (int i = 0; i < 10; ++i) {
      lowChar = Convert.ToChar(random.Next(0xDC00, 0xE000));
      highChar = Convert.ToChar(random.Next(0xD800, 0xDC00));
      charArray[i] = highChar;
      charArray[++i] = lowChar;
}
tw.WriteChars(charArray, 0, charArray.Length);

for (int i = 0; i < 10; ++i) {
      lowChar = Convert.ToChar(random.Next(0xDC00, 0xE000));
      highChar = Convert.ToChar(random.Next(0xD800, 0xDC00));
      tw.WriteSurrogateCharEntity(lowChar, highChar);
}

tw.WriteEndAttribute();
tw.WriteEndElement();
tw.Flush();
tw.Close();

XmlTextReader r = new XmlTextReader("SurrogatePair.xml");

r.Read();
r.MoveToFirstAttribute();
targetFile = new FileStream("SurrogatePairFromReader.xml",
       FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

tw = new XmlTextWriter(targetFile, null);
tw.Formatting = Formatting.Indented;
tw.WriteStartElement("root");
tw.WriteStartAttribute("test", null);
tw.WriteString(r.Value);
tw.WriteEndAttribute();
tw.WriteEndElement();
tw.Flush();
tw.Close();

// Load both result files into the DOM and compare.
XmlDocument doc1 = new XmlDocument();
XmlDocument doc2 = new XmlDocument();
doc1.Load("SurrogatePair.xml");
doc2.Load("SurrogatePairFromReader.xml");
if (doc1.InnerXml != doc2.InnerXml) {
      Console.WriteLine("Surrogate Pair test case failed");
}
在使用 WriteChars 方法(一次写出一个缓冲区的数据)写出时,输入中的代理项对可能
会在一个缓冲区内被意外拆分。由于代理项值是定义完善的,如果 WriteChars 遇到来自
较低范围或者较高范围的 Unicode 值,它将该值标识为代理项对的一半。当遇到
 WriteChars 将导致从拆分代理项对的缓冲区写入的情况时,将引发异常。使用
  IsHighSurrogate 方法检查缓冲区是否以高代理项字符结束。如果缓冲区中的最后一个
  字符不是高代理项,可以将该缓冲区传递给 WriteChars 方法。

请参见
概念
使用 XmlTextWriter 创建格式正确的 XML
XmlTextWriter 的 XML 输出格式设置
XmlTextWriter 的命名空间功能

#endif

#5


头痛,用wstring和wchar_t*结合起来的方式,在调试状态下解决了部分,但是这个小程序在Dos下传入参数时,中文字符不能全部匹配。
如:传入路径参数


D:\加密与解密\学习笔记\PECLASS\Debug>peclass -p c:\windows\sys中国加em32\cmd.exe

c:\windows\syst中国em32\cmd.exe
unsussec   ---不成功
D:\加密与解密\学习笔记\PECLASS\Debug
i:27
D:\密与解密\学习笔记\PECLASS\Debug
sussec---成功


同样两个加字,系统程序获取的能解析前匹配,而通过参数传入的就不行。

wregex wrx(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+?(\\.exe|\\.dll)$");//[\\w]{1,5})$");
wregex wrx1(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+");
两个正则基本相同,只是一个有扩展名的判断,不带中文的路径判断是准确的。

#6


D:\加密与解密\学习笔记\PECLASS\Debug>peclass -p c:\windows\syst中国加em32\cmd.exe

        红色金刚石的PE文件工具
c:\windows\syst中国加em32\cmd.exe
c:\windows\syst中国加em32\cmd.exe
c:\windows\syst中国加em32\cmd.exe
unsussec
D:\加密与解密\学习笔记\PECLASS\Debug
D:\加密与解密\学习笔记\PECLASS\Debug
i:27
D:\加密与解密\学习笔记\PECLASS\Debug
D:\加密与解密\学习笔记\PECLASS\Debug
sussec

#7


提醒楼主:
在字符串扫描替换问题域中,正则表达式不是万能的;而有限状态自动机是万能的。
参考《编译原理》中的词法分析和有限状态自动机。

#8


引用 7 楼 zhao4zhong1 的回复:
提醒楼主:
在字符串扫描替换问题域中,正则表达式不是万能的;而有限状态自动机是万能的。
参考《编译原理》中的词法分析和有限状态自动机。

谢谢!
我对正则不太懂,用得很少,这次算是认真学了一点点,希望多批评指正。

#9


引用 5 楼 jinggangshi 的回复:
头痛,用wstring和wchar_t*结合起来的方式,在调试状态下解决了部分,但是这个小程序在Dos下传入参数时,中文字符不能全部匹配。
如:传入路径参数


D:\加密与解密\学习笔记\PECLASS\Debug>peclass -p c:\windows\sys中国加em32\cmd.exe

c:\windows\syst中国em32\cmd.exe
unsussec   ---不成功
D:\加密与解密\学习笔记\PECLASS\Debug
i:27
D:\密与解密\学习笔记\PECLASS\Debug
sussec---成功


同样两个加字,系统程序获取的能解析前匹配,而通过参数传入的就不行。

wregex wrx(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+?(\\.exe|\\.dll)$");//[\\w]{1,5})$");
wregex wrx1(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+");
两个正则基本相同,只是一个有扩展名的判断,不带中文的路径判断是准确的。

这样传入的不是utf-16吧。

如果你是在学加解密的话,建议你还是用英文吧。别把时间浪费在字符集上面。

#10


命令行参数应该是多字节(char),先用MultiByteToWideChar转成宽字符(wchar)

#11


引用 9 楼 luciferisnotsatan 的回复:
Quote: 引用 5 楼 jinggangshi 的回复:

头痛,用wstring和wchar_t*结合起来的方式,在调试状态下解决了部分,但是这个小程序在Dos下传入参数时,中文字符不能全部匹配。
如:传入路径参数


D:\加密与解密\学习笔记\PECLASS\Debug>peclass -p c:\windows\sys中国加em32\cmd.exe

c:\windows\syst中国em32\cmd.exe
unsussec   ---不成功
D:\加密与解密\学习笔记\PECLASS\Debug
i:27
D:\密与解密\学习笔记\PECLASS\Debug
sussec---成功


同样两个加字,系统程序获取的能解析前匹配,而通过参数传入的就不行。

wregex wrx(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+?(\\.exe|\\.dll)$");//[\\w]{1,5})$");
wregex wrx1(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+");
两个正则基本相同,只是一个有扩展名的判断,不带中文的路径判断是准确的。

这样传入的不是utf-16吧。

如果你是在学加解密的话,建议你还是用英文吧。别把时间浪费在字符集上面。

问题是不可能避免中文,如果在中文路径的判断上都做不到,你读取文件都成问题,其他的都是空谈。

问题症结也找到了,可能是字符的结尾没有'\0'、或者在字符与'\0'之间还有“符号”,这可能就是汉字双字造成的。
暂时的解决方法,是在构造string可wstring之前,对char*或wchar_t*的内存清0,::memset或::wmemset.

#12


引用 10 楼 luciferisnotsatan 的回复:
命令行参数应该是多字节(char),先用MultiByteToWideChar转成宽字符(wchar)


这个做了,但很奇怪,开始是做了转换,一直不行,后来直接用string和char却成功了。
再往后写了些代码后,又不行了,然后彻底调整,全部转换成wstring,前对char*和wchar_t*置'\0',问题暂时解决。

可能的原因,某个函数中的char*和wchar_t*在使用中存在未置'\0'的情况。

#1


[\\u4e00-\\u9fa5] 这个是Unicode set utf-16的编码范围。

std::string str("是"); 窄字符串,肯定不是utf16的。用wstring试试 str::wstring str(L"是");

#2


引用 1 楼 luciferisnotsatan 的回复:
[\\u4e00-\\u9fa5] 这个是Unicode set utf-16的编码范围。

std::string str("是"); 窄字符串,肯定不是utf16的。用wstring试试 str::wstring str(L"是");


regex_match不认wstring:
error C2665: 'regex_match' : none of the 32 overloads can convert parameter 1 from type 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >'
执行 cl.exe 时出错.

#3


也用宽字符版本的

template <class charT, class traits = regex_traits<charT>, class Allocator = std::allocator<charT>  >

class basic_regex;

typedef basic_regex<char> regex;

typedef basic_regex<wchar_t> wregex;

#4


再正常不过的现象。
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
extern "C" HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = FALSE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
void ShowTheCursor() {
    CONSOLE_CURSOR_INFO cciCursor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
        cciCursor.bVisible = TRUE;
        SetConsoleCursorInfo(hStdOut, &cciCursor);
    }
}
int main() {
    HWND  hwnd;
    HDC   hdc;
    HFONT hfont;
    wchar_t wc[2];

    system("color F0");
    system("cls");
    HideTheCursor();
    hwnd  = GetConsoleWindow();
    hdc   = GetDC(hwnd);
    hfont = CreateFont(48,0,0,0,0,0,0,0,GB2312_CHARSET ,0,0,0,0,"宋体-方正超大字符集");
    SelectObject(hdc,hfont);
    wc[0]=0xD854u;
    wc[1]=0xDC00u;
    TextOutW(hdc,10,10,wc,2);
    DeleteObject(hfont);
    ReleaseDC(hwnd,hdc);
    getch();
    system("color 07");
    system("cls");
    ShowTheCursor();
    return 0;
}
#if 0
代理项或代理项对是一对共同表示单个字符的 16 位 Unicode 编码值。需要记住的关键一点是:
代理项对实际上是 32 位单个字符,不能再假定一个 16 位 Unicode 编码值正好映射到一个字符。

使用代理项对
代理项对的第一个值是高代理项,包含介于 U+D800 到 U+DBFF 范围内的 16 位代码值。
该对的第二个值是低代理项,包含介于 U+DC00 到 U+DFFF 范围内的值。通过使用代理项对,
16 位 Unicode 编码系统可以对已由 Unicode 标准定义的一百多万个其他字符 (220) 进行寻址。

在传递给 XmlTextWriter 方法的任何字符串中都可以使用代理项字符。不过,代理项字符在编写的
XML 中应该有效。例如,万维网联合会 (W3C) 建议不允许在元素或属性的名称中使用代理项字符。
如果字符串包含无效的代理项对,则引发异常。

另外,可以使用 WriteSurrogateCharEntity 写出与代理项对相对应的字符实体。字符实体以十六
进制格式写出,并用以下公式生成:

(highChar -0xD800) * 0x400 + (lowChar -0xDC00) + 0x10000

如果字符串包含无效的代理项对,则引发异常。下面的示例显示将代理项对作为输入的 WriteSurrogateCharEntity 方法。

C#复制
 // The following line writes &#x10000.
WriteSurrogateCharEntity ('\uDC00', '\uD800');
下面的示例生成一个代理项对文件,将其加载到 XmlReader 中,并用新的文件名保存文件。
然后,原始文件和新文件被加载回应用程序的 XML 文档对象模型 (DOM) 结构中以进行比较。

C#复制
 char lowChar, highChar;
char [] charArray = new char[10];
FileStream targetFile = new FileStream("SurrogatePair.xml",
      FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

lowChar = Convert.ToChar(0xDC00);
highChar = Convert.ToChar(0xD800);
XmlTextWriter tw = new XmlTextWriter(targetFile, null);
tw.Formatting = Formatting.Indented;
tw.WriteStartElement("root");
tw.WriteStartAttribute("test", null);
tw.WriteSurrogateCharEntity(lowChar, highChar);
lowChar = Convert.ToChar(0xDC01);
highChar = Convert.ToChar(0xD801);
tw.WriteSurrogateCharEntity(lowChar, highChar);
lowChar = Convert.ToChar(0xDFFF);
highChar = Convert.ToChar(0xDBFF);
tw.WriteSurrogateCharEntity(lowChar, highChar);

// Add 10 random surrogate pairs.
// As Unicode, the high bytes are in lower
// memory; for example, word 6A21 as 21 6A.
// The high or low is in the logical sense.
Random random = new Random();
for (int i = 0; i < 10; ++i) {
      lowChar = Convert.ToChar(random.Next(0xDC00, 0xE000));
      highChar = Convert.ToChar(random.Next(0xD800, 0xDC00));
      charArray[i] = highChar;
      charArray[++i] = lowChar;
}
tw.WriteChars(charArray, 0, charArray.Length);

for (int i = 0; i < 10; ++i) {
      lowChar = Convert.ToChar(random.Next(0xDC00, 0xE000));
      highChar = Convert.ToChar(random.Next(0xD800, 0xDC00));
      tw.WriteSurrogateCharEntity(lowChar, highChar);
}

tw.WriteEndAttribute();
tw.WriteEndElement();
tw.Flush();
tw.Close();

XmlTextReader r = new XmlTextReader("SurrogatePair.xml");

r.Read();
r.MoveToFirstAttribute();
targetFile = new FileStream("SurrogatePairFromReader.xml",
       FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

tw = new XmlTextWriter(targetFile, null);
tw.Formatting = Formatting.Indented;
tw.WriteStartElement("root");
tw.WriteStartAttribute("test", null);
tw.WriteString(r.Value);
tw.WriteEndAttribute();
tw.WriteEndElement();
tw.Flush();
tw.Close();

// Load both result files into the DOM and compare.
XmlDocument doc1 = new XmlDocument();
XmlDocument doc2 = new XmlDocument();
doc1.Load("SurrogatePair.xml");
doc2.Load("SurrogatePairFromReader.xml");
if (doc1.InnerXml != doc2.InnerXml) {
      Console.WriteLine("Surrogate Pair test case failed");
}
在使用 WriteChars 方法(一次写出一个缓冲区的数据)写出时,输入中的代理项对可能
会在一个缓冲区内被意外拆分。由于代理项值是定义完善的,如果 WriteChars 遇到来自
较低范围或者较高范围的 Unicode 值,它将该值标识为代理项对的一半。当遇到
 WriteChars 将导致从拆分代理项对的缓冲区写入的情况时,将引发异常。使用
  IsHighSurrogate 方法检查缓冲区是否以高代理项字符结束。如果缓冲区中的最后一个
  字符不是高代理项,可以将该缓冲区传递给 WriteChars 方法。

请参见
概念
使用 XmlTextWriter 创建格式正确的 XML
XmlTextWriter 的 XML 输出格式设置
XmlTextWriter 的命名空间功能

#endif

#5


头痛,用wstring和wchar_t*结合起来的方式,在调试状态下解决了部分,但是这个小程序在Dos下传入参数时,中文字符不能全部匹配。
如:传入路径参数


D:\加密与解密\学习笔记\PECLASS\Debug>peclass -p c:\windows\sys中国加em32\cmd.exe

c:\windows\syst中国em32\cmd.exe
unsussec   ---不成功
D:\加密与解密\学习笔记\PECLASS\Debug
i:27
D:\密与解密\学习笔记\PECLASS\Debug
sussec---成功


同样两个加字,系统程序获取的能解析前匹配,而通过参数传入的就不行。

wregex wrx(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+?(\\.exe|\\.dll)$");//[\\w]{1,5})$");
wregex wrx1(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+");
两个正则基本相同,只是一个有扩展名的判断,不带中文的路径判断是准确的。

#6


D:\加密与解密\学习笔记\PECLASS\Debug>peclass -p c:\windows\syst中国加em32\cmd.exe

        红色金刚石的PE文件工具
c:\windows\syst中国加em32\cmd.exe
c:\windows\syst中国加em32\cmd.exe
c:\windows\syst中国加em32\cmd.exe
unsussec
D:\加密与解密\学习笔记\PECLASS\Debug
D:\加密与解密\学习笔记\PECLASS\Debug
i:27
D:\加密与解密\学习笔记\PECLASS\Debug
D:\加密与解密\学习笔记\PECLASS\Debug
sussec

#7


提醒楼主:
在字符串扫描替换问题域中,正则表达式不是万能的;而有限状态自动机是万能的。
参考《编译原理》中的词法分析和有限状态自动机。

#8


引用 7 楼 zhao4zhong1 的回复:
提醒楼主:
在字符串扫描替换问题域中,正则表达式不是万能的;而有限状态自动机是万能的。
参考《编译原理》中的词法分析和有限状态自动机。

谢谢!
我对正则不太懂,用得很少,这次算是认真学了一点点,希望多批评指正。

#9


引用 5 楼 jinggangshi 的回复:
头痛,用wstring和wchar_t*结合起来的方式,在调试状态下解决了部分,但是这个小程序在Dos下传入参数时,中文字符不能全部匹配。
如:传入路径参数


D:\加密与解密\学习笔记\PECLASS\Debug>peclass -p c:\windows\sys中国加em32\cmd.exe

c:\windows\syst中国em32\cmd.exe
unsussec   ---不成功
D:\加密与解密\学习笔记\PECLASS\Debug
i:27
D:\密与解密\学习笔记\PECLASS\Debug
sussec---成功


同样两个加字,系统程序获取的能解析前匹配,而通过参数传入的就不行。

wregex wrx(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+?(\\.exe|\\.dll)$");//[\\w]{1,5})$");
wregex wrx1(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+");
两个正则基本相同,只是一个有扩展名的判断,不带中文的路径判断是准确的。

这样传入的不是utf-16吧。

如果你是在学加解密的话,建议你还是用英文吧。别把时间浪费在字符集上面。

#10


命令行参数应该是多字节(char),先用MultiByteToWideChar转成宽字符(wchar)

#11


引用 9 楼 luciferisnotsatan 的回复:
Quote: 引用 5 楼 jinggangshi 的回复:

头痛,用wstring和wchar_t*结合起来的方式,在调试状态下解决了部分,但是这个小程序在Dos下传入参数时,中文字符不能全部匹配。
如:传入路径参数


D:\加密与解密\学习笔记\PECLASS\Debug>peclass -p c:\windows\sys中国加em32\cmd.exe

c:\windows\syst中国em32\cmd.exe
unsussec   ---不成功
D:\加密与解密\学习笔记\PECLASS\Debug
i:27
D:\密与解密\学习笔记\PECLASS\Debug
sussec---成功


同样两个加字,系统程序获取的能解析前匹配,而通过参数传入的就不行。

wregex wrx(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+?(\\.exe|\\.dll)$");//[\\w]{1,5})$");
wregex wrx1(L"^([A-Za-z]:(\\\\|\\/))[\\w \\\\\\/]+");
两个正则基本相同,只是一个有扩展名的判断,不带中文的路径判断是准确的。

这样传入的不是utf-16吧。

如果你是在学加解密的话,建议你还是用英文吧。别把时间浪费在字符集上面。

问题是不可能避免中文,如果在中文路径的判断上都做不到,你读取文件都成问题,其他的都是空谈。

问题症结也找到了,可能是字符的结尾没有'\0'、或者在字符与'\0'之间还有“符号”,这可能就是汉字双字造成的。
暂时的解决方法,是在构造string可wstring之前,对char*或wchar_t*的内存清0,::memset或::wmemset.

#12


引用 10 楼 luciferisnotsatan 的回复:
命令行参数应该是多字节(char),先用MultiByteToWideChar转成宽字符(wchar)


这个做了,但很奇怪,开始是做了转换,一直不行,后来直接用string和char却成功了。
再往后写了些代码后,又不行了,然后彻底调整,全部转换成wstring,前对char*和wchar_t*置'\0',问题暂时解决。

可能的原因,某个函数中的char*和wchar_t*在使用中存在未置'\0'的情况。