C++正则表达式问题求助,在线等
大家好,请大家帮忙看看这段代码,这个正则表达式和字符串用正则表达式工具测是匹配成功的,但是到了程序中,一直是无法匹配的状态,大家帮忙看看,非常感谢
#include <iostream>
#include <regex.h>
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
int iFunMatchString (char *pcPattern,char *pcMark,vector<string> &pcGetMatched);
int main(){
char acPattern[512];
memset (acPattern ,0 , sizeof(acPattern));
sprintf ( acPattern ,"%s" ,"(([A-Za-z0-9]*)_([A-Za-z0-9_]*)-([0-9]{1}) ([A-Za-z 0-9_\\[\\]\\-,]*));");
char * pcMark = "He_Chi10_MX2-1 Abis TP 53[_53];major (2);communicationsAlarm;X721-lossOfFrame;190342 BSC-TRANS [101] LFA [2];();();((Alcatel Additional Information significant Additional Information is ) (Alcatel Additional Information significant -- OMC3 ALARM --) (urlAddressOnAlarmDictionary significant https://gxsesn045:7443helpd/cgi-bin/alarm_search.cgi?specific_problem=101002 190342));1;unAcknowledged (0);cleared (1);20120613031312.0Z;;1.3.12.2.1006.53.1.2.0.3.1014;0.0.13.3100.0.7.68#9/0.0.13.3100.0.7.20#1/0.0.13.3100.0.7.48#53;20120613092957.0Z;manualPurge (3);1;";
vector<string> pcGetMatched;
int i = iFunMatchString(acPattern,pcMark,pcGetMatched);
return 0;
}
int iFunMatchString (char *pcPattern,char *pcMark,vector<string> &pcGetMatched)
{
int iRet;
int iLoop ;
const int STRINGLEN = 1024;
const int SUBLEN = 64;
size_t sLen ;
regex_t sRegex ;
regmatch_t acMatched [SUBLEN] ;
char acErrBuffer [ STRINGLEN ] ;
char acMatchedTemp [STRINGLEN *5];
memset(acMatchedTemp,0,sizeof(acMatchedTemp));
memset ( acErrBuffer , 0 , sizeof ( acErrBuffer ) ) ;
iRet = regcomp ( &sRegex , pcPattern , REG_EXTENDED|REG_NEWLINE ) ;
if ( iRet != 0 )
{
sLen = regerror ( iRet , &sRegex , acErrBuffer , sizeof ( acErrBuffer ) ) ;
return -1 ;
}
iRet = regexec ( &sRegex , pcMark , ( size_t )SUBLEN , acMatched , 0 ) ;
cout<<"pcMark:"<<pcMark<<endl;
cout<<"acMatched:"<<acMatched<<endl;
if ( iRet == REG_NOMATCH )
{
//这里面会执行,函数返回
regfree ( &sRegex ) ;
cout<<"匹配失败,函数返回"<<endl;
return -1 ;
}
for ( iLoop = 0 ; iLoop < sRegex.re_nsub ; iLoop ++ )
{
sLen = acMatched [ iLoop + 1 ].rm_eo - acMatched [ iLoop + 1 ].rm_so ;
if ( sLen > sizeof (acMatchedTemp) )
memcpy ( acMatchedTemp , pcMark + acMatched [ iLoop + 1 ].rm_so , sizeof(acMatchedTemp)) ;
else
memcpy ( acMatchedTemp , pcMark + acMatched [ iLoop + 1 ].rm_so , sLen );
string tempStr = acMatchedTemp;
memset(acMatchedTemp,0,sizeof(acMatchedTemp));
pcGetMatched.push_back(tempStr);
}
regfree ( &sRegex ) ;
return acMatched[0].rm_eo ;
}
新人,没什么分,请大家帮忙指点一下
[解决办法]
posix正则库regex.h。
你直接贴一下 原文与希望匹配得到的内容, 另外注意:REG_NEWLINE的使用是否符合你的预期:
- C/C++ code
REG_NEWLINE Match-any-character operators don’t match a newline. A nonmatching list ([^...]) not containing a newline does not match a newline. Match-beginning-of-line operator (^) matches the empty string immediately after a newline, regardless of whether eflags, the execution flags of regexec(), contains REG_NOTBOL. Match-end-of-line operator ($) matches the empty string immediately before a newline, regardless of whether eflags contains REG_NOTEOL.