Regular expressions in C
Last Updated :
01 Nov, 2023
Prerequisite: How to write Regular Expressions?
A regular expression is a sequence of characters that is used to search pattern. It is mainly used for pattern matching with strings, or string matching, etc. They are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java, and Python.
Patterns in the POSIX Library
[] |
Used to find any of the characters or numbers specified between the brackets. |
[:number:] |
Used to find any digit. |
[:lower:] |
Used to find lowercase alphabets. |
[:word:] |
Used to find letters numbers and underscores. |
Creation of Regular Expression
For compiling or creating the regular expression regcomp() function is used. It takes three arguments:
Syntax:
regcomp(®ex, expression, flag)
where,
- regex is a pointer to a memory location where expression is matched and stored.
- expression is a string type
- flag to specify the type of compilation
Return Value: This returns the value as shown below:
- 0: when successful compilation is done.
- Error_code: When there is unsuccessful compilation of the expression.
Below is the illustration of the regcomp() function:
C
#include <regex.h>
#include <stdio.h>
int main()
{
regex_t reegex;
int value;
value = regcomp( &reegex, "[:word:]" , 0);
if (value == 0) {
printf ( "RegEx compiled successfully." );
}
else {
printf ( "Compilation error." );
}
return 0;
}
|
Output
RegEx compiled successfully.
Time complexity : O(n)
Auxiliary Space : O(1)
where n is the length of the pattern
Matching of Pattern using Regular Expression
The regexec() function is used to match a string against a pattern. It takes in five arguments:
- A precompiled pattern
- A string in which the pattern needs to be searched for.
- Information regarding the location of matches.
- Flags to specify a change in the matching behavior.
Syntax:
regexec(®ex, expression, 0, NULL, 0);
where, regex = precompiled pattern,
expression = pattern to be match in regex,
NULL = Information regarding location of the matches.
flag = to specify the change in matching behaviour
Return Value: This returns the value as shown below:
- 0: If there is a match.
- REG_NOMATCH: If there is no match.
Below is the illustration of the regexec() function:
C
#include <regex.h>
#include <stdio.h>
void print_result( int value)
{
if (value == 0) {
printf ( "Pattern found.\n" );
}
else if (value == REG_NOMATCH) {
printf ( "Pattern not found.\n" );
}
else {
printf ( "An error occurred.\n" );
}
}
int main()
{
regex_t reegex;
int value;
int value2;
value = regcomp( &reegex, "Welcome to GfG" , 0);
value = regexec( &reegex, "GeeksforGeeks" ,
0, NULL, 0);
value2 = regcomp( &reegex, "GeeksforGeeks" , 0);
value2 = regexec( &reegex, "GeeksforGeeks" ,
0, NULL, 0);
print_result(value);
print_result(value2);
return 0;
}
|
Output
Pattern not found.
Pattern found.
Time complexity : O(n)
Auxiliary Space : O(1)
where n is the length of the pattern
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...