Prerequiste: 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 complilation of the expression.
Below is the illustration of the regcomp() function:
// C program for illustration of regcomp() #include <regex.h> #include <stdio.h> // Driver Code int main() { // Variable to create regex regex_t reg; // Variable to store the return // value after creation of regex int value; // Function call to create regex value = regcomp(®, "[:word:]", 0); // If compilation is successfull if (value == 0) { printf("RegEx compiled successfully."); } // Else for Compilation error else { printf("Compilation error."); } return 0; } |
RegEx compiled successfully.
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 = precomplied 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 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 occured during Pattern // matching else { printf("An error occured.\n"); } } // Driver Code int main() { // Variable to store initial regex() regex_t reg; // Variable for return type int value; int value2; // Creation of regEx value = regcomp(®, "Welcome to GfG", 0); // Comparing pattern "GeeksforGeeks" with // string in reg value = regexec(®, "GeeksforGeeks", 0, NULL, 0); // Creation of regEx value2 = regcomp(®, "GeeksforGeeks", 0); // Comparing pattern "Geeks" // with string in reg value2 = regexec(®, "GeeksforGeeks", 0, NULL, 0); // Print the results print_result(value); print_result(value2); return 0; } |
Pattern not found. Pattern found.
Recommended Posts:
- Regular Expressions in Python
- Pointer Expressions in C with Examples
- How to validate MAC address using Regular Expression
- How to validate Indian driving license number using Regular Expression
- How to validate CVV number using Regular Expression
- Check whether two convex regular polygon have same center or not
- How to validate a domain name using Regular Expression
- How to validate GST (Goods and Services Tax) number using Regular Expression
- How to validate HTML tag using Regular Expression
- How to validate IFSC Code using Regular Expression
- How to validate MasterCard number using Regular Expression
- How to validate Visa Card number using Regular Expression
- How to validate Indian Passport number using Regular Expression
- Convert user input string into regular expression using JavaScript
- How to validate GUID (Globally Unique Identifier) using Regular Expression
- Check if an URL is valid or not using Regular Expression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


