用于VS 2008的简单C ++正则表达式库,没有Feature Pack

时间:2013-11-18 21:55:22

标签: c++ regex windows winapi mfc

我正在开发一个在没有Feature Pack的情况下在VS 2008中开发的MFC / C ++项目。 (我之所以没有使用这个功能包,是因为这个项目需要向后兼容Win2K,而这个包已经淘汰了。)

所以我想做一些简单的正则表达式匹配。

我做了一些搜索,我能找到的大部分内容要么不能包含在商业项目中(我的是),要么库本身对我的需求太重了(例如Boost.Regex。)< / p>

有什么建议吗?

3 个答案:

答案 0 :(得分:4)

如果我是你,我会使用Boost 1.54但不使用它们的库,只需使用它们的源代码 安装完整的增压分配。在您的项目中,通过选择所有
来添加C ++文件 ..\boost_1_54_0\libs\regex\src\*.*中的文件,当然还会添加标题路径。

正则表明,正则表达式src只是代码,并且很好地符合您的项目设置,因此您的项目不依赖于它们的库。

这在您的代码中占用的空间非常小。但是,您也可以复制大部分项目设置以创建正则表达式源代码的独立lib / dll,然后将其包含在您的其他项目中。

使用boost,商业代码没有许可问题,只要它不与它一起分发(即便如此,你只需要归属)。

我要告诉你,提升正则表达式是一个神话般的引擎。


<强> EDIT2: 如果将正则表达式源添加到项目中,则What libs do I need to include?的答案为NONE。没有库,因为所有代码都成为可执行文件的一部分。

您所要做的就是告诉您的项目安装发行版的目录(​​见下文)。您添加的源文件将包含所需的HPP文件。只是编译。

从我的代码来看,它看起来像是2-300K,这并不是那么糟糕 boost regex几乎都是模板类hpp文件,它很适合C ++ 你可以从我的样本中看到,我非常广泛地使用迭代器,它是最佳选择。

我使用了很多正则表达式。我将它们保存在myconst.h/cpp模型中,并包含在需要的位置。我还使用一个名为RegexFormat(website)的程序来管理它们。格式/压缩/扩展/调试和来回转换为C ++字符串。让它变得简单..只需检查,他们将价格降低到29美元。 (我在49岁时得到它,哦,好吧)。

还有很多方法可以使用boost regex 这是一个小的使用示例。我试图在没有神圣意义的情况下将其删除 它看起来仍然很大(抱歉,所以)。

不要犹豫,问你是否还有其他问题......

 Project Properties
 -------------------
 Configuration Properties ->
       C/C++ ->
           General ->
             Additional Include Directories ->
                F:\Dev\boost_1_54_0    ( <-where you install the distribution )



 //////////////////////////////////////////////
 // ======== myconst.h ==========
 #pragma once

 #ifndef _myconst
 #define _myconst

 //
 #include <boost/regex.hpp>
 #include <string> 
 #include <iostream> 

 // ------------------------

 using namespace std;
 using namespace stdext;
 using namespace boost;

 // ---------------------------------------------
 typedef std::string::const_iterator SITR;

 #define MOD    regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m 
 #define MODx   regex_constants::perl | boost::regex::no_mod_s | boost::regex::no_mod_m | regex_constants::mod_x
 #define MODs   regex_constants::perl | boost::regex::no_mod_m | regex_constants::mod_s
 #define MODxs  regex_constants::perl | boost::regex::no_mod_m | regex_constants::mod_s | regex_constants::mod_x

 #define MODm   regex_constants::perl | boost::regex::no_mod_s  
 #define MODxm  regex_constants::perl | boost::regex::no_mod_s| regex_constants::mod_x
 #define MODsm  regex_constants::perl | regex_constants::mod_s
 #define MODxsm regex_constants::perl | regex_constants::mod_s | regex_constants::mod_x

 // Common regexes

 extern boost::regex  TextLine;
 extern boost::regex  TRI_TextLineReplace;
 extern boost::regex  TextLineReplace;
 extern boost::regex  BlankLinesReplace; 
 extern boost::regex  StripBoundryWsp;

 #endif _myconst  // End _myconst
 //
 //////////////////////////////////////////////


 //////////////////////////////////////////////
 // ======== myconst.cpp ==========
 #include "stdafx.h"
 #include "myconst.h"

  boost::regex  TextLine (
    " (?| ( [^\\r\\n]* ) \\r\\n | ( [^\\r\\n]+ ) \\z )   "
     , MODx);
  boost::regex  TRI_TextLineReplace (                 // with support to escape trigraph '??x' sequence
    " ( [\\\\\"] | \\?(?=\\?) | (?<=\\?)\\? )  "
     , MODx);
  boost::regex  TextLineReplace (                     // no support to escape trigraph '??x' sequence
    " ( [\\\\\"] )  "
     , MODx);
  boost::regex StripBoundryWsp ("\\A \\s*(?=\\S) (.+?) (?<=\\S)\\s* \\z", MODxs);

  boost::regex  BlankLinesReplace (
    " (?:     "
    "       (?> \\A  [^\\S\\r\\n]* (?: \\z | (?=\\r\\n) ) )         "
    "    |  (?> (?<=\\r\\n) [^\\S\\r\\n]* (?: \\z | (?=\\r\\n) ) )  "
    "  )                                                            "
     , MODx);
 //
 //////////////////////////////////////////////


 //////////////////////////////////////////////
 // ======== myeditor.cpp ==========
 #include "stdafx.h"
 #include "myconst.h"
 #include "myeditor.h"
 #include "makecstrdlg.h"


 LRESULT MyEditor::OnMsgMakeCstring(WPARAM /*wp*/, LPARAM /*lp*/)
 {
     CMakeCstrDlg dlg;
     if (dlg.DoModal() == IDOK)
     {
         if ( dlg.m_bType2 )
         {
             CString mfcstr;
             GetWindowText( mfcstr );
             string strSrc = mfcstr;

             GetMakeCstrType2( strSrc, dlg.bTrigraph );          

             SetWindowText( strSrc.c_str() );
         }
     }
     SetFocus(); 
     return 0;
 }

 void MyEditor::GetMakeCstrType2( string& strSrc, bool bTrigraph )
 {
    if ( strSrc.length() == 0 )
    {
        strSrc.assign("\"\"");  // '""'
        return;
    }


    boost::regex Replacer;

    if ( bTrigraph )
        Replacer = TRI_TextLineReplace;
    else
        Replacer = TextLineReplace;

    string strNewSrc = "";
    string tmp;

    SITR _Mstart;
    SITR _Mend;
    boost::smatch _M;

    _Mstart = strSrc.begin();
    _Mend   = strSrc.end();

    while ( boost::regex_search ( _Mstart, _Mend, _M, TextLine ) )
    {
        tmp.assign( _M[1].first, _M[1].second );
        tmp  = boost::regex_replace ( tmp, Replacer, "\\\\$1" );

        strNewSrc.append( "\"" + tmp );
        strNewSrc.append( "\\n\"\r\n" );

        _Mstart = _M[0].second;
    }
    strSrc = strNewSrc;
 }
 //
 //////////////////////////////////////////////

编辑:如果您使用boost regex,下面将帮助您

预处理器:这些boost定义可以很好地运行

   Boost Defines (in Project Settings)
   =================================
   BOOST_ALL_NO_LIB
   BOOST_REGEX_NON_RECURSIVE
   BOOST_REGEX_BLOCKSIZE=32768
   BOOST_REGEX_MAX_BLOCKS=8192
   BOOST_REGEX_MAX_CACHE_BLOCKS=4096

这修复了一个恼人的警告,因为MS无法控制自己的头文件

   =================================================

   #include <intsafe.h>
   #include <stdint.h>

   CAUSES THIS ->


   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(72): warning C4005: 'INT8_MIN' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(144) : see previous definition of 'INT8_MIN'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(73): warning C4005: 'INT16_MIN' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(146) : see previous definition of 'INT16_MIN'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(74): warning C4005: 'INT32_MIN' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(148) : see previous definition of 'INT32_MIN'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(76): warning C4005: 'INT8_MAX' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(167) : see previous definition of 'INT8_MAX'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(77): warning C4005: 'INT16_MAX' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(171) : see previous definition of 'INT16_MAX'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(78): warning C4005: 'INT32_MAX' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(176) : see previous definition of 'INT32_MAX'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(79): warning C4005: 'UINT8_MAX' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(168) : see previous definition of 'UINT8_MAX'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(80): warning C4005: 'UINT16_MAX' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(173) : see previous definition of 'UINT16_MAX'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(81): warning C4005: 'UINT32_MAX' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(178) : see previous definition of 'UINT32_MAX'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(149): warning C4005: 'INT64_MIN' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(152) : see previous definition of 'INT64_MIN'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(150): warning C4005: 'INT64_MAX' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(184) : see previous definition of 'INT64_MAX'
   1>f:\program files\microsoft visual studio 10.0\vc\include\stdint.h(151): warning C4005: 'UINT64_MAX' : macro redefinition
   1>          c:\program files\microsoft sdks\windows\v7.0a\include\intsafe.h(189) : see previous definition of 'UINT64_MAX'




   Fix 1 (preferred):
   ====================================================

   In "\microsoft visual studio 10.0\vc\include\stdint.h" undefine them before they  are re-defined


   #undef INT8_MIN
   #undef INT16_MIN
   #undef INT32_MIN
   #undef INT8_MAX
   #undef INT16_MAX
   #undef INT32_MAX
   #undef UINT8_MAX
   #undef UINT16_MAX
   #undef UINT32_MAX
   #undef INT64_MIN
   #undef INT64_MAX
   #undef UINT64_MAX


   Fix 2:
   ====================================================
   In "\boost\config\compiler\visualc.hpp"  undefine BOOST_HAS_STDINT_H


   //#if _MSC_VER >= 1600
   #undef BOOST_HAS_STDINT_H
   //#  define BOOST_HAS_STDINT_H
   //#endif

答案 1 :(得分:2)

Boost是一个很好的答案,但如果您以前从未使用过Boost,那么可能是较小的实现就足够了并且副作用可能较少。

Atl Server(在Codeplex上)在atlrx.h中也有一个正则表达式引擎 也在MSDN

中记录

您也可以找到示例代码here

答案 2 :(得分:0)

Henry Spencer编写的旧的但广泛使用的正则表达式库,可能包含在数百种产品中。这是您可以获得它的众多地方之一:

http://www.arglist.com/regex/