Regular expressions in C
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
Expression | Description |
---|---|
[] | 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
// C program for illustration of regcomp() #include <regex.h> #include <stdio.h> // Driver Code int main() { // Variable to create regex regex_t reegex; // Variable to store the return // value after creation of regex int value; // Function call to create regex value = regcomp( &reegex, "[:word:]" , 0); // If compilation is successful if (value == 0) { printf ( "RegEx compiled successfully." ); } // Else for Compilation error else { printf ( "Compilation error." ); } return 0; } |
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
// C program to illustrate the regexec() function #include <regex.h> #include <stdio.h> // Function to print the result void print_result( int value) { // If pattern found if (value == 0) { printf ( "Pattern found.\n" ); } // If pattern not found else if (value == REG_NOMATCH) { printf ( "Pattern not found.\n" ); } // If error occurred during Pattern // matching else { printf ( "An error occurred.\n" ); } } // Driver Code int main() { // Variable to store initial regex() regex_t reegex; // Variable for return type int value; int value2; // Creation of regEx value = regcomp( &reegex, "Welcome to GfG" , 0); // Comparing pattern "GeeksforGeeks" with // string in reg value = regexec( &reegex, "GeeksforGeeks" , 0, NULL, 0); // Creation of regEx value2 = regcomp( &reegex, "GeeksforGeeks" , 0); // Comparing pattern "Geeks" // with string in reg value2 = regexec( &reegex, "GeeksforGeeks" , 0, NULL, 0); // Print the results print_result(value); print_result(value2); return 0; } |
Pattern not found. Pattern found.
Time complexity : O(n)
Auxiliary Space : O(1)where n is the length of the pattern
Please Login to comment...