Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Translate Perl to C

by nardo (Friar)
on Aug 06, 2001 at 01:54 UTC ( #102350=note: print w/replies, xml ) Need Help??

in reply to Translate Perl to C

In addition to strtok which was already mentioned, your platform may provide a regcomp function for regular expressions:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <regex.h> int main(void) { char *header = "This is a sample header\nNumber: 12345\nThis is ano +ther line\n"; char *number = NULL; regex_t re; regmatch_t pmatch[2]; int retval; regcomp(&re, "^Number: (.+)", REG_EXTENDED | REG_NEWLINE); retval = regexec(&re, header, sizeof(pmatch)/sizeof(*pmatch), pmatc +h, 0); if(retval == 0 && pmatch[1].rm_so != -1) { size_t len = pmatch[1].rm_eo - pmatch[1].rm_so; number = malloc(len + 1); memcpy(number, header + pmatch[1].rm_so, len); number[len] = '\0'; printf("Number is %s\n", number); } else { printf("Match failed\n"); } regfree(&re); free(number); return 0; }
No error checking is done and number is stored as a string, atoi()/strtol() family can turn it into an integer.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://102350]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2022-05-20 08:43 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (73 votes). Check out past polls.