2016-07-06 34 views
1

wie url /[a-z0-9]/phpmyadmin/? Wenn URL entspricht Regex = /^\/([a-z0-9])+\/phpmyadmin/g wie Gruppen für weitere Operation char erfassen?wie pcre library zu verwenden url in c programm

zum Beispiel, ich habe diese

char *url = "/zs0099/phpmyadmin"; 

ich es gegen regex /^\/([a-z0-9])+\/phpmyadmin/g übereinstimmen soll, wenn es passt dann

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

eine gute Suchmaschine ist dein Freund http://www.mitchr.me/SS/exampleCode/AUPG/pcre_example.c.html – dvhh

Antwort

0

schnellen Schuss aus der Hüfte (ohne PCRE), blank C (kleine Zustandsmaschine)

#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 : ""); 
} 

mögliche Probleme:
"// phpMyAdmin" gilt (könnte mit einem weiteren Zustand leicht behoben werden, wobei der erste Zustand isdigit nur akzeptiert() und ISNUMBER(), keine '/')
"/ phpMyAdmin" ungültig ist