Thread: How Can I make a POSIX/C regex non-greedy?
so have code trying extract part of url, part between http:// , first /. this:
assume argv[1] http://www.google.com/stuff. trying match www.google.com part. [^/] in there, still matches everything. (i.e, www.google.com/stuff) appreciate can provide.code:#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <string.h> #include <regex.h> int main (int argc, char **argv) { regex_t regex; int reti; char msgbuf[100]; size_t nmatch = 2; regmatch_t pmatch[2]; /* match between http:// , * first / */ reti = regcomp (®ex, "http://\\([^/]*\\)/", 0); if (reti) { fprintf (stderr, "failed compile regular expression"); exit (exit_failure); } reti = regexec (®ex, argv[1], nmatch, pmatch, 0); char *match = strndup(argv[1]+pmatch[1].rm_so, pmatch[1].rm_eo); printf ("%s\n", match); return 0; }
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk How Can I make a POSIX/C regex non-greedy?
Ubuntu
Comments
Post a Comment