使用MFC编写正则表达式

由与CSDN本人检索不到合适的教我在MFC上面编写正则表达式的教程,所以只好硬着头皮啃英文文档并写程序验证了。下面是一个MFC使用正则表达式的教程。不知道什么是正则表达式的自己百度去。

首先在VS后来的版本中,包含正则表达式的部分从MFC里面抽离开来,要使用正则表达式要先下载并添加到项目中。

关于下载首先在百度云或者官网“http://pan.baidu.com/s/1nv8hRpn”找到并下载这么一个库,然后将include目录包含到vc++附加目录里面,

之后在工程中#include "atlrx.h"


然后就开始我们的正则之旅


首先打开说明微软的文档 

https://msdn.microsoft.com/en-us/library/k3zs4axe(VS.80).aspx

需要翻译的可以参考这篇

http://blog.csdn.net/flyfish1986/article/details/5877838




CAtlRegExp<> reUrl; //新建正则表示式对象
    // Five match groups: scheme, authority, path, query, fragment
    REParseError status = reUrl.Parse(
        "({[^:/?#]+}:)?(//{[^/?#]*})?{[^?#]*}(?{[^#]*})?(#{.*})?" );//编写正则表达式
    if (REPARSE_ERROR_OK != status)
    {
        // Unexpected error.防止未知错误
        return 0;
    }

    CAtlREMatchContext<> mcUrl;//新建一个用来存储匹配结果的对象
    if (!reUrl.Match(
"http://search.microsoft.com/us/Search.asp?qu=atl&boolean=ALL#results",
        &mcUrl))//调用match匹配命令匹配正则表达式
    {
        // Unexpected error.没有匹配到的时候进这里
        return 0;
    }

    for (UINT nGroupIndex = 0; nGroupIndex < mcUrl.m_uNumGroups;
         ++nGroupIndex)
    {
        const CAtlREMatchContext<>::RECHAR* szStart = 0;
        const CAtlREMatchContext<>::RECHAR* szEnd = 0;
        mcUrl.GetMatch(nGroupIndex, &szStart, &szEnd);//将结果拿出来,匹配的结果是前面一段减去后面一段
        ptrdiff_t nLength = szEnd - szStart;
        printf_s("%d: \"%.*s\"\n", nGroupIndex, nLength, szStart);
    }

从上面的例子可以看出,我们只要改正则表达式还有匹配那一部分还有最后的输出那里改成我们要的处理就可以了,那个输出我编译的时候报错,所以用TRACE输出查看了。

看起来十分的顺利就可以用正则表达式了,可是后面发现,这个正则表达式对挺多功能不支持的,比如前向搜索,后向搜索还有替换,而且限定出现次数也不像其他正则表达式一样用的是{1,1}。所以首先我们来结合微软的文档对进行例子中的正则表达式进行解析

({[^:/?#]+}:)?(//{[^/?#]*})?{[^?#]*}(?{[^#]*})?(#{.*})?" )
首先第一部分({[^:/?#]+}:)?

最外层的部分()?表示这个子表达式可以为0或者1

?:Indicates that the preceding expression is optional: it matches once or not at all (for example, [0-9][0-9]? matches "2" and "12").

较里面的部分{[^:/?#]+}:

表示匹配在冒号前面的除了:/?#的所有字符 一个大括号表示一个组,最后的结果会以组1,组2,组3这样子在下面输出



+:Indicates that the preceding expression matches one or more times (for example,  [0-9]+  matches "1", "13", "456", and so on).

^:If this metacharacter occurs at the start of a character class, it negates the character class. A negated character class matches any character except those inside the brackets (for example, [^abc] matches all characters except "a", "b", and "c").

If ^ is at the beginning of the regular expression, it matches the beginning of the input (for example, ^[abc] will only match input that begins with "a", "b", or "c").



{}:Indicates a match group. The actual text in the input that matches the expression inside the braces can be retrieved through the CAtlREMatchContext object.

写了个小例子测试一下





最后就是获取我们要的结果了,在上面的例子中得到结果之后由于是const CAtlREMatchContext<>::RECHAR*格式的数据,所以要进行数据转换,如
CString tttresult;
tttresult.Format(_T("%s"),szStart);
tttresult=tttresult.Mid(0,nLength);
ttt.Format(_T("szresult is %s\r\n"),tttresult);

TRACE(ttt);

用left也可以。

好的,经过这篇从安装到参考例程,到自己编写正则表达式再到得到数据基础的就这么多了希望能给找不到头绪的人一点思路。拜

CAtlRegExp<> reUrl;
    // Five match groups: scheme, authority, path, query, fragment
    REParseError status = reUrl.Parse(
        "({[^:/?#]+}:)?(//{[^/?#]*})?{[^?#]*}(?{[^#]*})?(#{.*})?" );

    if (REPARSE_ERROR_OK != status)
    {
        // Unexpected error.
        return 0;
    }

    CAtlREMatchContext<> mcUrl;
    if (!reUrl.Match(
"http://search.microsoft.com/us/Search.asp?qu=atl&boolean=ALL#results",
        &mcUrl))
    {
        // Unexpected error.
        return 0;
    }

    for (UINT nGroupIndex = 0; nGroupIndex < mcUrl.m_uNumGroups;
         ++nGroupIndex)
    {
        const CAtlREMatchContext<>::RECHAR* szStart = 0;
        const CAtlREMatchContext<>::RECHAR* szEnd = 0;
        mcUrl.GetMatch(nGroupIndex, &szStart, &szEnd);

        ptrdiff_t nLength = szEnd - szStart;
        printf_s("%d: \"%.*s\"\n", nGroupIndex, nLength, szStart);
    }

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值