这是网上的一篇教程。我试着做了一次。但是结果编译不过。请大家指点指点。看错误在什么地方。
错误提示:
Checking project dependencies...
Compiling Project2.cbproj (Debug configuration)
[BCC32 Error] Unit2.cpp(20): E2451 Undefined symbol 'edt1'
Full parser context
Unit2.cpp(19): parsing: void _fastcall TForm2::Button1Click(TObject *)
[BCC32 Error] Unit2.cpp(21): E2451 Undefined symbol 'edt2'
Full parser context
Unit2.cpp(19): parsing: void _fastcall TForm2::Button1Click(TObject *)
[BCC32 Error] Unit2.cpp(21): E2285 Could not find a match for 'regex_match<iterator,charT,traits>(undefined,wregex)'
Full parser context
Unit2.cpp(19): parsing: void _fastcall TForm2::Button1Click(TObject *)
[BCC32 Error] Unit2.cpp(21): E2268 Call to undefined function 'regex_match'
Full parser context
Unit2.cpp(19): parsing: void _fastcall TForm2::Button1Click(TObject *)
Failed
代码:
//---------------------------------------------------------------------------
#include <boost/regex.hpp>
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
boost::wregex regExp(edt1->Text.c_str());
if( regex_match(edt2->Text.c_str(), regExp ) )
{
ShowMessage(L"匹配成功!");
}
else
{
ShowMessage(L"匹配失败!");
}
}
//---------------------------------------------------------------------------
在C++Builder 2010中使用boost的正则表达式
在新发布的C++Builder 2010中,已经带了boost 1.39,
安装的时候,选上就可以了。
总的来说,C++ Builder 2010这点是做的不错的,省了我们为了使用boost,要自己下载,并长时间的编译所带来的痛苦。最重要的是,还不一定成功。
开始:菜单->File->New->VCL Forms Applications C++ Builder
这样,我们就有了一个带有Form的C++Builder工程。
然后在上面,拖上两个TEdit和TLabel控件和一个TButtom控件,并修改相应的属性,结果如下:
然后双击“匹配”这个按钮,就到产生单击事件,代码实现如下
void __fastcall TForm1::btn1Click(TObject *Sender)
{
boost::wregex regExp(edt2->Text.c_str());
if( regex_match(edt1->Text.c_str(), regExp ) )
{
ShowMessage(L"匹配成功!");
}
else
{
ShowMessage(L"匹配失败!");
}
}
记得,要在CPP的头部,加入正则表达式的头文件
#include <boost/regex.hpp>
然就可以编译运行了,结果如果下:
OK,完成。
在C++Builder下面,使用boost的正则表达式,非常的轻松!
由于boost的强大,使用C++Builder也非常的强大!