c语言正则表达式 windows,C语言中的正则表达式:示例?

While the answer above is good, I recommend using PCRE2. This means you can literally use all the regex examples out there now and not have to translate from some ancient regex.

I made an answer for this already, but I think it can help here too..

// YOU MUST SPECIFY THE UNIT WIDTH BEFORE THE INCLUDE OF THE pcre.h

#define PCRE2_CODE_UNIT_WIDTH 8

#include

#include

#include

#include

int main(){

bool Debug = true;

bool Found = false;

pcre2_code *re;

PCRE2_SPTR pattern;

PCRE2_SPTR subject;

int errornumber;

int i;

int rc;

PCRE2_SIZE erroroffset;

PCRE2_SIZE *ovector;

size_t subject_length;

pcre2_match_data *match_data;

char * RegexStr = "(?:\\D|^)(5[1-5][0-9]{2}(?:\\ |\\-|)[0-9]{4}(?:\\ |\\-|)[0-9]{4}(?:\\ |\\-|)[0-9]{4})(?:\\D|$)";

char * source = "5111 2222 3333 4444";

pattern = (PCRE2_SPTR)RegexStr;// <<<<< This is where you pass your REGEX

subject = (PCRE2_SPTR)source;// <<<<< This is where you pass your bufer that will be checked.

subject_length = strlen((char *)subject);

re = pcre2_compile(

pattern, /* the pattern */

PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */

0, /* default options */

&errornumber, /* for error number */

&erroroffset, /* for error offset */

NULL); /* use default compile context */

/* Compilation failed: print the error message and exit. */

if (re == NULL)

{

PCRE2_UCHAR buffer[256];

pcre2_get_error_message(errornumber, buffer, sizeof(buffer));

printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset,buffer);

return 1;

}

match_data = pcre2_match_data_create_from_pattern(re, NULL);

rc = pcre2_match(

re,

subject, /* the subject string */

subject_length, /* the length of the subject */

0, /* start at offset 0 in the subject */

0, /* default options */

match_data, /* block for storing the result */

NULL);

if (rc < 0)

{

switch(rc)

{

case PCRE2_ERROR_NOMATCH: //printf("No match\n"); //

pcre2_match_data_free(match_data);

pcre2_code_free(re);

Found = 0;

return Found;

// break;

/*

Handle other special cases if you like

*/

default: printf("Matching error %d\n", rc); //break;

}

pcre2_match_data_free(match_data); /* Release memory used for the match */

pcre2_code_free(re);

Found = 0; /* data and the compiled pattern. */

return Found;

}

if (Debug){

ovector = pcre2_get_ovector_pointer(match_data);

printf("Match succeeded at offset %d\n", (int)ovector[0]);

if (rc == 0)

printf("ovector was not big enough for all the captured substrings\n");

if (ovector[0] > ovector[1])

{

printf("\\K was used in an assertion to set the match start after its end.\n"

"From end to start the match was: %.*s\n", (int)(ovector[0] - ovector[1]),

(char *)(subject + ovector[1]));

printf("Run abandoned\n");

pcre2_match_data_free(match_data);

pcre2_code_free(re);

return 0;

}

for (i = 0; i < rc; i++)

{

PCRE2_SPTR substring_start = subject + ovector[2*i];

size_t substring_length = ovector[2*i+1] - ovector[2*i];

printf("%2d: %.*s\n", i, (int)substring_length, (char *)substring_start);

}

}

else{

if(rc > 0){

Found = true;

}

}

pcre2_match_data_free(match_data);

pcre2_code_free(re);

return Found;

}

Install PCRE using:

wget https://ftp.pcre.org/pub/pcre/pcre2-10.31.zip

make

sudo make install

sudo ldconfig

Compile using :

gcc foo.c -lpcre2-8 -o foo

Check my answer for more details.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值