Skip to main content

Thread: How Can I make a POSIX/C regex non-greedy?


so have code trying extract part of url, part between http:// , first /. this:

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 (&regex, "http://\\([^/]*\\)/", 0);          if (reti) {                  fprintf (stderr, "failed compile regular expression");                  exit (exit_failure);          }          reti = regexec (&regex, argv[1], nmatch, pmatch, 0);            char *match = strndup(argv[1]+pmatch[1].rm_so, pmatch[1].rm_eo);            printf ("%s\n", match);            return 0;  }
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.

quote posted iteckid view post
so have code trying extract part of url, part between http:// , first /. this:

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 (&regex, "http://\\([^/]*\\)/", 0);          if (reti) {                  fprintf (stderr, "failed compile regular expression");                  exit (exit_failure);          }          reti = regexec (&regex, argv[1], nmatch, pmatch, 0);            char *match = strndup(argv[1]+pmatch[1].rm_so, pmatch[1].rm_eo);            printf ("%s\n", match);            return 0;  }
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.
"so" , "eo" sound mean "start offset" , "end offset". use "so" way, use "eo" if count.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk How Can I make a POSIX/C regex non-greedy?


Ubuntu

Comments

Popular posts from this blog

how to devide a circle into equal parts

"Could not fill because there are not enough opaque source pixels" - not solved by any other thread

Why can't I change the billing info for my account?