Regular expressions in C - GeeksforGeeks

03 May, 2022
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  
Creation of Regular Expression For compiling or creating the regular expression regcomp() function is used. It takes three arguments: Syntax:  
regcomp(&regex, expression, flag)
where,  
regex is a pointer to a memory location where expression is matched and stored.expression is a string typeflag to specify the type of compilation
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
// C program for illustration of regcomp()#include <regex.h>#include <stdio.h> // Driver Codeint 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.
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 patternA 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.
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(&regex, 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 behaviourReturn 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 resultvoid 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 Codeint 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.
rafaelrutzsantos
varshagumber28
anikakapoor
smarmstr
regular-expression
Articles
C Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Understanding "extern" keyword in C
SQL Interview Questions
Mutex vs Semaphore
Time Complexity and Space Complexity
Strings in C
Arrow operator -> in C/C++ with Examples
Header files in C/C++ and its uses
C Program to read contents of Whole File
UDP Server-Client implementation in C