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
// 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;
}
OutputRegEx 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;
}
OutputPattern not found.
Pattern found.
Time complexity : O(n)
Auxiliary Space : O(1)
where n is the length of the pattern
Similar Reads
Number System Conversion in C
Number system conversion is a fundamental concept in computer science and programming. It involves changing the representation of a number from one base to another, such as converting a decimal number to binary or a hexadecimal number to binary.In this article, we will create a console program in th
8 min read
putchar() function in C
The putchar(int ch) method in C is used to write a character, of unsigned char type, to stdout. This character is passed as the parameter to this method. Syntax: int putchar(int ch) Parameters: This method accepts a mandatory parameter ch which is the character to be written to stdout. Return Value:
1 min read
User-Defined Function in C
A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no hea
6 min read
nextafter() Function in C
In C, the nextafter() is a standard library function that is used to find the next representable floating-point value after a given number in the direction of another specified number.In this article, we will learn how to use the nextafter() function in C and its variant for different floating point
2 min read
strlen() function in c
The strlen() function in C calculates the length of a given string. The strlen() function is defined in string.h header file. It doesn't count the null character '\0'. Syntax of C strlen() The syntax of strlen() function in C is as follows: size_t strlen(const char* str);Parameters The strlen() func
1 min read
fprintf() in C
fprintf is used to print content in file instead of stdout console. int fprintf(FILE *fptr, const char *str, ...); Example: C // C Program for the above approach #include<stdio.h> int main() { int i, n=2; char str[50]; //open file sample.txt in write mode FILE *fptr = fopen("sample.txt
1 min read
strrev() function in C
The strrev() function is a built-in function in C and is defined in string.h header file. The strrev() function is used to reverse the given string. Syntax:char *strrev(char *str);Parameter:str: The given string which is needed to be reversed.Returns: This function doesn't return anything but the re
2 min read
Types of User Defined Functions in C
A user-defined function is one that is defined by the user when writing any program, as we do not have library functions that have predefined definitions. To meet the specific requirements of the user, the user has to develop his or her own functions. Such functions must be defined properly by the u
4 min read
%n in scanf() in C with Example
In C, %n is a special format specifier. In the case of printf() function the %n assign the number of characters printed by printf(). When we use the %n specifier in scanf() it will assign the number of characters read by the scanf() function until it occurs. Key points: It is an edit conversion code
2 min read
strnset() function in C
The strnset() function is a builtin function in C and it sets the first n characters of a string to a given character. If n is greater than the length of string, the length of string is used in place of n. Syntax: char *strnset(const char *str, char ch, int n); Parameters: str: This is the original
2 min read