如何匹配网址/[a-z0-9]/phpmyadmin/
?
如果url匹配regex = /^\/([a-z0-9])+\/phpmyadmin/g
如何将组捕获到char以进行进一步操作?
例如,我有这个
char *url = "/zs0099/phpmyadmin";
我希望将其与正则表达式/^\/([a-z0-9])+\/phpmyadmin/g
匹配
如果匹配则
char *token = "zs0099"; //(how to get this value to assigned here)
答案 0 :(得分:0)
从臀部快速射击(但没有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(),no'/')
“/ phpmyadmin”无效