In my current project, i need to search out the GUIDs from some certain strings. This is a difficult job if you want to written with the tradition C++. But with C++/CLI, we can benefit from the powerful .NET library. I have to admit that mixing the C++/CLI code with the C++ code is a bit confusing.
#include "stdafx.h"
#using <system.dll>
#using <system.data.dll>
using namespace System;
using namespace System::Text::RegularExpressions;
using namespace System::Runtime::InteropServices;
int _tmain(int argc, _TCHAR* argv[])
{
String ^ sDelivDataXML = "gp:GUID=\"7F57069D-DD5C-48E9-B746-6FAFF0271B9A\" gp:Included=\"true\"gp:Component gp:GUID=\"02023E2A-A4ED-45B8-9900-D3A144F09C82\" ";
String^ pat = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}";
Regex^ r = gcnew Regex( pat,RegexOptions::Compiled);
Match^ m = r->Match(sDelivDataXML);
int matchCount = 0;
while ( m->Success )
{
Console::WriteLine( "{0}", m->Value );
m = m->NextMatch();
}
return 0;
}
I will not cover the grama or any other knowledge of C++/CLI or the regular expression in this article. If you have any questions about the String^ or how to write the regular expression, just google it.
The Output :
7F57069D-DD5C-48E9-B746-6FAFF0271B9A02023E2A-A4ED-45B8-9900-D3A144F09C82ok, enjoy it!