gpt4 book ai didi

c - c程序中如何使用pcre库匹配url

转载 作者:太空宇宙 更新时间:2023-11-04 04:29:19 24 4
gpt4 key购买 nike

如何匹配 url /[a-z0-9]/phpmyadmin/ ?如果 url 匹配 regex = /^\/([a-z0-9])+\/phpmyadmin/g如何捕获组以进行进一步操作?

比如我有这个

char *url = "/zs0099/phpmyadmin";

我想将它与正则表达式匹配 /^\/([a-z0-9])+\/phpmyadmin/g如果匹配则

char *token = "zs0099"; //(how to get this value to assigned here)

最佳答案

从臀部快速射门(但没有 pcre),裸 C(小状态机)

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int isMatch( char *url, char *p ) {
int state= 0;
while ( *url != 0 ) {
switch (state) {
// first state: char must be '/'
case 0:
if (*url != '/') return 0;
state=1;
url++;
break;
// only characters and numbers are valid, another '/' ends this
case 1:
if (isalpha(*url) || isdigit(*url)) { *p++=*url; url++; break; }
if (*url == '/') { url++; state=2; break; }
return 0; // not alhanum nor '/' --> not valid;
break;
case 2:
*p++=0;
if (strcmp(url, "phpmyadmin")==0) return 1;
return 0;
}
}
return 0;
}

int main() {
char part[256];

char *x1 = "/019sasA/phpmyadmin";
int matches = isMatch(x1, part);
printf ("%s, %i --> %s \n", x1, matches , matches ? part : "");

char *x2 = "/019sasA/phpadmin";
matches = isMatch(x2, part);
printf ("%s, %i --> %s \n", x2, matches , matches ? part : "");

char *x3 = "/019sa,sA/phpmyadmin";
matches = isMatch(x3, part);
printf ("%s, %i --> %s \n", x3, matches , matches ? part : "");
}

可能的问题:
“//phpmyadmin”是有效的(可以很容易地用另一个状态修复,其中第一个状态只接受 isdigit() 和 isnumber(),没有 '/')
“/phpmyadmin”无效

关于c - c程序中如何使用pcre库匹配url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38219093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com