我有一个关键字需要循环以匹配wchar_t字符串。
字符串是
5698 @#$%notepad ^& 343235 / chrome $ 56& 6556firefox $%#$ 23
,关键字为
记事本,chrome,firefox
我尝试循环它总是返回FALSE并且当我看到cpu使用时。它如此广泛,约占50%。
这个代码。
unsigned int i;
wchar_t * keyword[] = {L"chrome", L"notepad", L"firefox");
for (i = 0; i < 3; i++)
{
wchar_t * s = L"5698@#$%notepad^&343235/ chrome $56&6556firefox$%#$23";
wcmatch m;
wchar_t regex[MAX_PATH];
swprintf(regex, MAX_PATH, L"%s%s", L".+", keyword[i]);
wregex r(regex);
if (regex_search(s, m, r))
{
cout << "It's found" << endl;
}
else
{
cout << "It's not found" << endl;
}
我的真实项目代码。枚举进程并匹配进程并返回bool值。
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <Psapi.h>
#include <string>
#include <regex>
using namespace std;
int main()
{
FILE *pFile;
wchar_t *file = L"d:\\a.txt";
wchar_t line[100][MAX_PATH];
unsigned int a = 0;
if (_wfopen_s(&pFile, file, L"r, ccs = UNICODE") == 0)
{
while (fgetws(line[a], 100, pFile))
{
a++;
}
}
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return 1;
}
cProcesses = cbNeeded / sizeof(DWORD);
wchar_t jszProcessName[4096] = { 0 };
for (i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
wchar_t szProcessName[MAX_PATH] = TEXT("Unknown");
DWORD processID = aProcesses[i];
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID);
if (NULL != hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
&cbNeeded))
{
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szProcessName) / sizeof(TCHAR));
wcsncat_s(jszProcessName,szProcessName , 1000);
}
}
}
}
unsigned int c;
for (c = 0; c < a; c++)
{
wchar_t * s = jszProcessName;
wcmatch m;
wchar_t regex[MAX_PATH];
swprintf(regex, MAX_PATH, L"%s%s", L".+", line[c]);
wregex r(regex);
if (regex_search(s, m, r))
{
cout << "It's found" << endl;
}
else
{
cout << "It's not found" << endl;
}
}
return 0;
}