Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,
Can you please suggest a site which actually contains full tutorial about using regular expressions through regex.h in C on linux.

Appreciate your response.

Regards,
Posted

Here ( "Example of POSIX regex in C"[^] ) an example of using POSIX regular expressions in C. Here the GNU documentation about "10.3 Regular Expression Matching"[^] the regular expressions C library. With Google you may find many general tutorials on regular epressions.
 
Share this answer
 
Comments
Espen Harlinn 30-Oct-11 9:46am    
5'ed - what happened to RTFS & RTFM?
CPallini 30-Oct-11 16:43pm    
:-) Thank you.
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

C++
#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]; //A list of the matches in the string (a list of 1)
	//Compare the string to the expression
	//regexec() returns 0 on match, otherwise REG_NOMATCH
	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; //Our compiled expression
	//1. Compile our expression.
	//Our regex is "-?[0-9]+(\\.[0-9]+)?". I will explain this later.
	//REG_EXTENDED is so that we can use Extended regular expressions
	rv = regcomp(&exp, "-?[0-9]+(\\.[0-9]+)?", REG_EXTENDED);
	if (rv != 0) {
		printf("regcomp failed with %d\n", rv);
	}
	//2. Now run some tests on it
	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");
	//3. Free it
	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, ...
 
Share this answer
 
v2
Comments
Espen Harlinn 30-Oct-11 9:47am    
5'ed too :)
[no name] 30-Oct-11 9:51am    
Can you please tell me the difference between [:alnum] and [[:alnum]].
Does [:alnum] means only 1 alpha or number ?
Andrew Brock 30-Oct-11 9:57am    
Perhaps what you are after is a tutorial on regular expressions in general, rather than using the C functions for them.

I will update my answer to give a basic explanation on regular expressions.

To answer this question however, I am not familiar with [[:alnum]], but if I had to guess it would not work at all.
[no name] 30-Oct-11 10:13am    
i am being very specific to C as i need to write programs.
Try looking at the documentation:
http://gnuwin32.sourceforge.net/packages/regex.htm[^]

Examples are included with the source, and the documentation is fairly decent - for this pupose the GnuWin32 download it's equally applicable to linux/*nix.

Most likely this is already somewhere on your system :)

Best regards
Espen Harlinn
 
Share this answer
 
Comments
[no name] 30-Oct-11 9:27am    
thank you very much for the information but i dont see any tutorial.
Espen Harlinn 30-Oct-11 9:47am    
I would guess the supplied examples would do nicely :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900