正則表達式(Regular Expressions),又被稱為regex或regexp,是一種十分簡便、靈活的文本處理工具。它可以用來精確地找出某文本中匹配某種指定規則的內容。
關於正則表達式的教程,可以參考這裡。
在C/C++中常用的正則表達式庫有GNU Regex Library, Boost.Regex, PCRE, PCRE++。這四個庫中,後面兩個是有關系,其它都是各自己獨立的,是不同的實現。今天主要用GNU Regex Library。
幾個主要函數
(1)regcomp:
[cpp]
int regcomp(regex_t *preg, const char *pattern, int cflags)
功能:將要進行匹配的正則表達式pattern進行編譯,做匹配前的准備工作
參數: preg, 輸出參數,用來保存編譯後的正則表達式結果
pattern, 輸入參數,傳入要進行編譯的正則表達式的字符串
cflags, 輸入參數,用來指定正則表達式匹配過程中的一些選項
返回值:編譯成功返回0,失敗返回非0的錯誤碼
int regcomp(regex_t *preg, const char *pattern, int cflags)
功能:將要進行匹配的正則表達式pattern進行編譯,做匹配前的准備工作
參數: preg, 輸出參數,用來保存編譯後的正則表達式結果
pattern, 輸入參數,傳入要進行編譯的正則表達式的字符串
cflags, 輸入參數,用來指定正則表達式匹配過程中的一些選項
返回值:編譯成功返回0,失敗返回非0的錯誤碼
(2)regexec:
[cpp]
int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[],
int eflags)
功能:用來檢測字符串string是否匹配正則表達式preg
參數: preg, 輸入參數,在(1)regcomp中編譯好的正則表達式規則
string, 輸入參數,用來被匹配的字符串
nmatch, 輸入參數,用來指定pmatch參數所對應的數組的長度
pmatch, 輸出參數,用來輸出在string中匹配preg的具體位置
eflag, 輸入參數,用來指定正則表達式匹配過程中的一些選項
返回值: 如果string匹配preg所指定的規則,則返回0, 否則返回非0
int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[],
int eflags)
功能:用來檢測字符串string是否匹配正則表達式preg
參數: preg, 輸入參數,在(1)regcomp中編譯好的正則表達式規則
string, 輸入參數,用來被匹配的字符串
nmatch, 輸入參數,用來指定pmatch參數所對應的數組的長度
pmatch, 輸出參數,用來輸出在string中匹配preg的具體位置
eflag, 輸入參數,用來指定正則表達式匹配過程中的一些選項
返回值: 如果string匹配preg所指定的規則,則返回0, 否則返回非0
(3)regerror:
[plain] view plaincopyprint?size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
功能:用來把在regcompt和regexec中產生的錯誤碼轉化成字符串形式的錯誤信息
參數: errcode, 輸入參數,在regcomp或regexec調用中返回的錯誤碼
preg, 輸入參數,與錯誤碼所對應的編譯過的正則表達式結構
errbuf, 輸出參數,用來返回錯誤信息的buffer,如果buffer不夠所需大小,錯誤信息將被截斷
errbuf_size, 輸入參數,返回錯誤信息的buffer的大小
返回值: 如果errbuf_size為0,那麼regerror返回錯誤信息所需要的buffer的大小
size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
功能:用來把在regcompt和regexec中產生的錯誤碼轉化成字符串形式的錯誤信息
參數: errcode, 輸入參數,在regcomp或regexec調用中返回的錯誤碼
preg, 輸入參數,與錯誤碼所對應的編譯過的正則表達式結構
errbuf, 輸出參數,用來返回錯誤信息的buffer,如果buffer不夠所需大小,錯誤信息將被截斷
errbuf_size, 輸入參數,返回錯誤信息的buffer的大小
返回值: 如果errbuf_size為0,那麼regerror返回錯誤信息所需要的buffer的大小
(4)regfree:
[plain] view plaincopyprint?void regfree (regex_t *preg)
功能: 用來釋放由regcomp編譯時生成的preg結構所占用的內存
參數: preg, 輸入參數,由regcomp編譯時生成的正則表達的結構指針
返回值: 無
void regfree (regex_t *preg)
功能: 用來釋放由regcomp編譯時生成的preg結構所占用的內存
參數: preg, 輸入參數,由regcomp編譯時生成的正則表達的結構指針
返回值: 無
實例-郵箱匹配
[plain]
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
int main(int argc,char** argv)
{
int status,i;
int cflags=REG_EXTENDED;
regmatch_t pmatch[1];
const size_t nmatch=1;
regex_t reg;
const char *pattern ="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*";
char buf[256];
regcomp(®,pattern,cflags);
while(true)
{
printf("input your email address:\n");
gets(buf);
if((status=regexec(®,buf,nmatch,pmatch,0))==0)
{
printf("Correct\n");
//print match part
for(i=pmatch[0].rm_so;i<pmatch[0].rm_eo;++i)
putchar(buf[i]);
printf("\n");
regfree(®);
break;
}
else
{
printf("Error address,input again:\n");
}
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
int main(int argc,char** argv)
{
int status,i;
int cflags=REG_EXTENDED;
regmatch_t pmatch[1];
const size_t nmatch=1;
regex_t reg;
const char *pattern ="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*";
char buf[256];
regcomp(®,pattern,cflags);
while(true)
{
printf("input your email address:\n");
gets(buf);
if((status=regexec(®,buf,nmatch,pmatch,0))==0)
{
printf("Correct\n");
//print match part
for(i=pmatch[0].rm_so;i<pmatch[0].rm_eo;++i)
putchar(buf[i]);
printf("\n");
regfree(®);
break;
}
else
{
printf("Error address,input again:\n");
}
}
return 0;
}