致开

A simple C++/CLI code snippet

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-6FAFF0271B9A
02023E2A-A4ED-45B8-9900-D3A144F09C82

ok, enjoy it!


posted on 2008-12-15 16:31 Xiaxi 阅读(371) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿(1)

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜