No need for a full tutorial, they are simple to use.
See the
online MAN page[
^] for a full description
1. Compile an expression (this example is to match integer or decimal numbers)
2. Use that expression
3. Free the compiled expression when you are done with it
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#define MAX_MATCHES 1 //The maximum number of matches allowed in a single string
void match(regex_t *pexp, char *sz) {
regmatch_t matches[MAX_MATCHES]; if (regexec(pexp, sz, MAX_MATCHES, matches, 0) == 0) {
printf("\"%s\" matches characters %d - %d\n", sz, matches[0].rm_so, matches[0].rm_eo);
} else {
printf("\"%s\" does not match\n", sz);
}
}
int main() {
int rv;
regex_t exp; rv = regcomp(&exp, "-?[0-9]+(\\.[0-9]+)?", REG_EXTENDED);
if (rv != 0) {
printf("regcomp failed with %d\n", rv);
}
match(&exp, "0");
match(&exp, "0.");
match(&exp, "0.0");
match(&exp, "10.1");
match(&exp, "-10.1");
match(&exp, "a");
match(&exp, "a.1");
match(&exp, "0.a");
match(&exp, "0.1a");
match(&exp, "hello");
regfree(&exp);
return 0;
}
EDIT:
Now for a basic explanation of regular expressions.
The original regular expressions were pretty basic. They included:
One of [AB] one of A or B
Not one of [^AB] anything but A and B (new line may or may not be included, implementation or option specific)
Zero or more A* any number of A's including 0
Group (A) Used for grouping things like OR
Any character . Any single character (not including new line)
See
Wikipedia[
^] for a full list
Then came extended regular expressions, which added many useful features:
Zero or one A? Either zero or 1 A (same as "A\{0,1\}" in basic regex)
Or A|B Either A or B
One or more A+ One or more A's (same as "AA*" in basic regex)
See
Wikipedia[
^] for a full list
Then there are predefined lists of characters, as a shorthand of writing
[ABC]
, see
Wikipedia[
^] for a full list. These can be implementation specific, however the GNU one supports all sets listed at Wikipedia.
The easiest way to build a regular expression is to break it down into its simplest parts, and them combine them together.
For example we want an expression that will find any words starting with A and ending with B OR starting with B and ending with A.
First, break it into its 2 components
1. A.*B
2. B.*A
Now group them together with an OR
(A.*B)|(B.*A)
Note that this is different to
[AB].*[AB]
, which will also match AA, AxA, BB, ...