Thread: nightmare!! I really need help with this!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    9

    nightmare!! I really need help with this!

    Hello,

    I'm making a network application and I need regex. I found out that I can use regex.h for that. Unfortunately it is not as simple is in PHP using preg_match_all();

    Here are my questions:
    1. Will I be able to use regex.h on Windows without being dependent on some library?
    2. I'm using libcurl for the networking side. Is it possible to compile libcurl into the exe, so my app is not dependent?
    3. Basicly I'm looking for an alternative to preg_match_all() function in PHP, I need function to be able to match for example this regex: "<div>(.*?)</div>" and return me all the elements. Is there an simpler way to do this or is there a snippet I could use?


    So far I got this code below. Basicly I need to get array with values (title1, title2). I have no idea how do extract multiple times, so I get both titles, not just one. Secondly, rm_so and rm_eo return 4 and 56 - the first <title> tag and the very last</title> tag, it should return the next </title> tag, instead of the last occurrence, I don't get why.. Finally, why doesn't it return the starting point of the wild card? instead it returns the starting point of the pattern (in this case the '<title>' tag).

    I have been searching google for hours and can't find anything that explains what I'm trying to accomplish. Any help is much appreciated!! Thank you!
    Code:
    #include <sys/types.h>
    #include <regex.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
            regex_t regex;
    	regmatch_t pm;
    
            int reti;
    	int len;
    	
            reti = regcomp(&regex, "<title>(.*)</title>", REG_EXTENDED | REG_ICASE);
          
            reti = regexec(&regex, "dsfg<title>title 1</title> adfa\nsdf<title>title2</title>asd", 4, &pm, 0);
    	if (pm.rm_so==-1){
    	  puts("Can't find it!");
    	  
    	} else { puts("got it!"); 
    	    len = pm.rm_eo - pm.rm_so;
    
    	    printf("str %i\n", pm.rm_so);
    	    printf("end %i\n", pm.rm_eo);
    
    	}
    
    
    	regfree(&regex);
    
            return 0;
    }

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    1. No. You will need "some library" (There are several available).
    2. Yes. You want to do static linking. Search libcurl's website or forums for instructions on how to do this.
    3. Go to regcomp and check out the example near the bottom of the page.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Never used it. Maybe it wants something like below (using an array of matches, since the regular expression might match a lot of cases). Just a sample code, no idea if it works.
    Code:
    #include <sys/types.h>
    #include <regex.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
            regex_t regex;
    	regmatch_t* pm;
    
            int reti;
    	int len;
    	
            reti = regcomp(&regex, "<title>(.*)</title>", REG_EXTENDED | REG_ICASE);
            int matches = regex.re_nsub;
            pm = malloc(matches * sizeof(*pm));
            reti = regexec(&regex, "dsfg<title>title 1</title> adfa\nsdf<title>title2</title>asd", matches, pm, 0);
    	if (pm.rm_so==-1){
    	  puts("Can't find it!");
    	  
    	} else { puts("got it!"); 
    	    len = pm.rm_eo - pm.rm_so;
                for (i = 0; i < matches; ++i)
                {
    	      printf("str %i\n", pm[i].rm_so);
    	      printf("end %i\n", pm[i].rm_eo);
                }
    	}
    
    
    	regfree(&regex);
            free(pm);
            return 0;
    }
    Don't know much about regular expressions so if it was specified to match the first occurance, dunno why it doesn't.

    Just add a number to go to the wildcard position (like the lenght of <title>)

    You could integrate the library in your .exe, but that is not that simple. But you could copy all the files that are needed for the library in a folder then do something like
    Code:
    #include "regex.h"
    (instead of <..>)

    The files needed would probably be regex.h and regex.o or you would make a Makefile with regex.h and regex.cpp.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    NeonBlack, the regex library can be compiled into exe too, correct? Thanks I got it!

    C_ntua, I didn't know regex.re_nsub stores the number of maches, thanks!


    So the problem I'm facing now is that my regex doesn't work as expected. I tested my regex on this website RegExr.


    Pattern:
    Code:
    <title>(.*?)</title>
    Text:
    Code:
    asd <title>title1</title> asdf <title>title2</title>as df <title>title3</title>sdf
    The output - This is what I need!
    asd <title>title1</title> asdf <title>title2</title>as df <title>title3</title>sdf

    However, this is what I'm getting with my code:
    asd <title>title1</title> asdf <title>title2</title>as df <title>title3</title>sdf

    In the output below, the end shows the location of the last </title> instead of the first one. The re.re_nsub show only one match. It acts like I'm doing "<title>(.*)</title>", instead of "<title>(.*?)</title>" as a pattern. What am I doing wrong? Thank you!

    Output:
    found: 1
    str: 4
    end: 71
    Source:
    Code:
    #include <sys/types.h>
    #include <regex.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
      
        char buffer[] = "asd <title>title1</title> asdf <title>title2</title>as df <title>title3</title>sdf";
          
        regex_t re;
        regmatch_t pm;
        int error;
    
          
        (void) regcomp (&re, "<title>(.*?)</title>", REG_EXTENDED);
        error = regexec (&re, &buffer[0], 1, &pm, 0);
    
        printf("found: %i\n", re.re_nsub);
    
        printf("str: %i\n", pm.rm_so);
        printf("end: %i\n", pm.rm_eo);
    
      return 0;
    }
    Last edited by codebot; 09-18-2010 at 09:36 PM.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Anyone?!

    I just need to answer to the last question why is my regex failing, thank you!

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    9
    Quote Originally Posted by Bayint Naung View Post
    Can you please point out how can I fix the code? Thanks!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you read the link? Was there something specific you didn't understand? Does it still not work after trying what was written there?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HTML tables with firefox and IE nightmare
    By MisterSako in forum Tech Board
    Replies: 6
    Last Post: 03-01-2006, 01:46 PM
  2. Replies: 2
    Last Post: 11-28-2003, 11:50 AM
  3. Printing nightmare
    By oskilian in forum Windows Programming
    Replies: 4
    Last Post: 01-27-2003, 11:01 PM
  4. 2 Dimensional Array Nightmare.
    By gurgletook in forum C Programming
    Replies: 1
    Last Post: 01-04-2003, 06:21 PM
  5. determining bank holidays = nightmare
    By rotis23 in forum C Programming
    Replies: 6
    Last Post: 12-04-2002, 04:37 AM