-
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.
-
Just use a library - example http://www.pcre.org/
-
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.
-
$ in a regex is the end of line anchor. You sure that's what you want?
-
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.
-
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