A regular expression is a sequence of characters used to match a pattern to a string. The expression can be used for searching text and validating input.
Remember, a regular expression is not the property of a particular language.
POSIX is a well-known library used for regular expressions in C.
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. |
The regcomp()
function is used to compile a regular expression. According to the official documentation, it takes three arguments:
It returns a 0 upon successful compilation, and an error code otherwise.
#include<stdio.h> // Importing the POSIX regex library #include <regex.h> int main() { regex_t regex; int return_value; return_value = regcomp(®ex,"[:lower:]",0); if (return_value == 0){ printf("Regular expression compiled successfully."); } else{ printf("Compilation error."); } return 0; }
The regexec()
function is used to match a string against a pattern. According to the official documentation, it takes in five arguments:
It returns a 0 if there is a match, and a REG_NOMATCH
error otherwise.
#include<stdio.h> // Importing the POSIX regex library #include <regex.h> void print_result(int return_value){ if (return_value == 0){ printf("Pattern found.\n"); } else if (return_value == REG_NOMATCH){ printf("Pattern not found.\n"); } else{ printf("An error occured.\n"); } } int main() { regex_t regex; int return_value; int return_value2; return_value = regcomp(®ex,"ice",0); return_value = regexec(®ex, "icecream", 0, NULL, 0); return_value2 = regcomp(®ex,"ice",0); return_value2 = regexec(®ex, "frozen yoghurt", 0, NULL, 0); print_result(return_value); print_result(return_value2); return 0; }
scanf()
Using a regular expression to accept only letters as input.
#include<stdio.h> #include<stdlib.h> int main() { char name[15]; // Taking a name as an input. // name can only include alphabets scanf("%[a-zA-Z]",name); printf("%s",name); return 0; }
Enter the input below
RELATED TAGS
View all Courses