下面是我在一个函数中验证是否是一个最多3位小数的数字的正则表达式实例
//添加头文件#include <pcre.h>
//入参:正则,要进行匹配的文本
bool _fastcall TDataModule1::RegEXMatch(char * strRegEX,char * strText)
{
const char *pszErr;
int nErrOffset;
pcre *re = pcre_compile(strRegEX,0,&pszErr,&nErrOffset,NULL); // 先准备表达式
if(re==NULL)
{
//printf("compile error at:%d, %s", nErrOffset, pszErr);
return false ;
}
int ovector[30]; // 数量由szReg决定,
int len = strlen(strText);
int rc = pcre_exec(re, NULL, strText, len, 0, ovector, 30); // 执行匹配
free(re);
if(rc != -1)
{
return true;
}else
{
return false;
}
}
//这个不好用
bool isPrice(AnsiString strPrice)
{
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
Variant Axl = Variant::CreateObject("VBScript.RegExp"); //利用脚本里的正则
Axl.PS("Global", true);//全局匹配
Axl.PS("Pattern", "^[0-9]+(.[0-9]{0,3})?$"); //写入正则表达式
Variant rtn = Axl.FN("Execute", strPrice.c_str());//开始执行
AnsiString str = "";
for (int i=0; i != rtn.PG("Count"); i++)
{
str = ((rtn.PG("Item",i).PG("Value")));//取出匹配的东东
}
if(str.IsEmpty())
{
return false;
}else
{
return true;
}
}