Thread: regex in c (posix regex)

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    regex in c (posix regex)

    Hi,

    I have a very simple question. How do i match a regex of :
    a) (please disregard this question )
    one or more digits

    Example:
    Code:
    char text[] = "x34c"
    char reg[]="x([0-9]{2})c";
    
    regex(reg,text);
    this will match my two digits after x but how to make it match all the digits between x and c given that ia don't know how many digit there are

    b)
    real number


    my regex function is as follows:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <string.h>
    #include <regex.h>
    
    #include <stdlib.h>
    #define NUM_MATCHES 4
    
    
    int regex (char *reg, char *text, int *regout){
    	
      regex_t myreg;
      regmatch_t mymatch[NUM_MATCHES];
      int rm, i,r=0;
      char myom[1024];
      rm = regcomp (&myreg, reg, REG_EXTENDED) ; 
      rm =regexec (&myreg, text, NUM_MATCHES, mymatch, 0);
      for (i = 1; !rm && i <= myreg.re_nsub; i++){
          r++;
          strncpy (myom, text + mymatch[i].rm_so, mymatch[i].rm_eo - mymatch[i].rm_so);
          myom[mymatch[i].rm_eo - mymatch[i].rm_so] = '\0';
          regout[i-1] = atoi(myom);
      }
    
      regfree (&myreg);
      return r -1;
    }
    
    int main (){
     int regout[2], i;
     int r;
     char text[] = "x34c"
     char reg[]="x([0-9]{2})c";
    
      r = regex(reg,argv[1],regout);
      printf("r:%d\n",r);
    
      for(i=0;i<r;i++){
    	  printf("digit:%d\n",  regout[i]);
      }
    P.S.

    also would this be the right (fastest) way to load a file of this type into memory:

    file example:

    Code:
    #type:12
    #tex:HHt6TT
    $$jksdfhusd7swdf76t6dfs77::CH1
    51 [tab] 75
    23 [tab] 7548
    57 [tab] 78
    56 [tab] 7
    3 [tab] 711
    1 [tab] 71
    #type:1
    #tex:HGtz7
    $$soookldio343s77::CH1
    46 [tab] 78
    26 [tab] 67
    57 [tab] 45
    59 [tab] 23
    23 [tab] 72
    89 [tab] 74
    [tab] is a tab separator. I need to upload into my memory table only the type number (1 from #type:1) and the two column table (numbers). I was thinking of regex but would that be the best way to go ??
    Last edited by baxy; 11-16-2012 at 06:27 AM. Reason: stupidity, PS

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by baxy View Post
    also would this be the right (fastest) way to load a file of this type into memory:
    Is your file as regularly formatted as your example?
    If yes, you could just loop through the file line by line using fgets() and then use sscanf() for reading the parts you want.
    Pseudocode:
    Code:
    while not at the end of file
        get number from first line of section (#type:xxx)
        skip two lines
        get numbers from next 6 lines
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Posix Regex in C
    By march5th in forum C Programming
    Replies: 13
    Last Post: 12-28-2008, 09:59 AM
  2. regex
    By John_L in forum C Programming
    Replies: 4
    Last Post: 05-27-2008, 06:31 PM
  3. using regex
    By _izua_ in forum C++ Programming
    Replies: 4
    Last Post: 08-10-2007, 04:25 PM
  4. <regex.h> regex syntax in C
    By battersausage in forum C Programming
    Replies: 7
    Last Post: 03-24-2004, 01:35 PM
  5. posix regex
    By jnsk in forum Linux Programming
    Replies: 2
    Last Post: 03-12-2004, 02:37 PM