Thread: Regular expressions

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Regular expressions

    Hi there;

    I know what regular expressions are, and have used very simple ones before - but I don't know if they can be used in C, or how to use them. I do know, though, that regex would save me a heck of a lot of trouble in parsing a slightly regular string (tokenising).

    What I am doing, is checking for the following regular expression:

    anystring*$anyint,*$anyint,*$anyint

    Where there is a *, there can be any number of whitespace.

    I suppose I am going against cboard ettiquette here and asking for a bit of a write out, but I don't know where to start looking (as I asked before, I don't even know if regex can be used in C).

    Googling attempts seem to throw 1000s of C++ string regex functions at me but no C..

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Just use a library - example http://www.pcre.org/
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    34
    Regular expressions are nice, but I don't think I'd use a library to deal with whitespace. You could easily just use a C function like sscanf() that will ignore whitespace. Something like:

    Code:
    const char line[] = "blah  123,     456,   789";
    char str[50];
    int i1, i2, i3;
    
    sscanf(line, "%s %d, %d, %d", str, &i1, &i2, &i3);
    
    printf("anystring: %s\n"
           "anyint1: %d\n"
           "anyint2: %d\n"
           "anyint3: %d\n", str, i1, i2, i3);
    or if you were reading from a file, you could use fscanf() which is the same thing, but it uses a file for its first parameter. In a way, C has functions that use a regular expression, like sscanf, but it's just not as complex as something like Perl style regex's. So I'd suggest looking more into the different string functions C. Anyway, good luck.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    $ in a regex is the end of line anchor. You sure that's what you want?

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    34
    hmm, I just figured he didn't know much about regular expressions and meant that those were variables. I think those *'s would more likely become \s+ if it was a real regular expression also.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    NOTE:
    Code:
    * represents -> 0 or more elements Ex: set{E, a ,aa,aaa,aaaa,.....} where Ereprents empty string
    
    + represents -> 1 or more elements Ex: set{a,aa,aaa,aaaa,....}
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regular expressions [Boost]
    By Desolation in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2006, 10:10 PM
  2. Help please: regular expressions in C++
    By reivaj7999 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2005, 01:11 AM
  3. Regular expressions
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-23-2005, 09:36 PM
  4. Regular Expressions
    By Korn1699 in forum C# Programming
    Replies: 4
    Last Post: 01-12-2005, 12:50 AM
  5. regular expressions help
    By axon in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-09-2004, 07:16 PM