Linux模式比對問題的研究

來源:互聯網
上載者:User

#include
#include
#include
#include
#include

//對輸入的模式與待匹配的字串進行匹配;成功返回0;
//pattern : 模式
// buf : 待匹配的字串;
int regexp(char *pattern, char * buf)

{
regex_t reg;
int status = 0;
int cflags = REG_EXTENDED|REG_NOSUB;
char error[32];

/* 編譯Regex*/
status = regcomp (, pattern, cflags);
if (status != 0)
{
regerror(status, , error, sizeof(error));
printf("regcomp ERROR -> %s: pattern '%s'\n", pattern, error);
return -1;
}

//進行匹配
status = regexec(, buf, 0, NULL, 0);
if(status != 0)
{
if(status != REG_NOMATCH)
{
regerror(status, , error, sizeof(error));
printf("regexec -> NON REG_NOMATCH -> ERROR -> status = %d: '\n", error);
status = -2;
}
else if(status == REG_NOMATCH)
{
printf("regexec -> REG_NOMATCH -> ERROR -> status = %d: '\n", error);
status = -3;
}
}
else
{
printf("regexec -> REG_NOMATCH -> SUCCESS -> pattern = %s, buf = %s \n", pattern, buf);
status = 0;
}

/* 輸出處理結果 */
//如果有必要可以輸出匹配的結果;

/* 釋放Regex */
regfree();

//返回處理結果;
return status;

}

/* 主程式 */

int main(int argc, char** argv)

{
//char * pattern = "^[0-9]*[1-9][0-9]*$";
char *pattern = "^[0-9]+$";
//char *pattern = "^\\d+$";
char *buf = "00000";

int a = regexp(pattern, buf);
printf("main -> INFO -> a = %d \n", a);
}
註:不支援Regex的控制。

     Linux好學堂, www.linuxhao.com,是學習Linux應用開發綜合性技術網站,類目包括Linux安裝指南,Linux系統管理,Linux網路應用,Linux伺服器技術,Linux編程開發,Linux下載中心,並提供Linux視頻教程,Linux培訓教程,Linux技術資料免費下載與資源共用等服務。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.