//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <regexp.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// BCB使用正则表达式的例子
void __fastcall TForm1::btn1Click(TObject *Sender)
{
// 定义正则表达式,除了 | 以外的连续字符串
String szReg = "[^|]+";
// 原始数据
String szStr = "|项目代码1|项目代码2|项目代码3|项目代码4";
// 构造正则表达式对象
TRegexp regex(szReg.c_str());
size_t len = 0;
size_t pos = 0;
while (true)
{
// 得到子字符串在原始字符串中起始位置pos 和 长度 len ,一开始从0位置开始找
pos = regex.find(szStr.c_str(), &len, pos);
// 如果得到一个unsigned字符的最大值,认为已经到达末尾
if (pos == size_t(-1))
{
break;
}
// 显示子串
String s = szStr.SubString(pos + 1 ,len);
ShowMessage(s);
// 移动pos
pos += len;
}
}
//---------------------------------------------------------------------------