Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
regex
pattern matching

How to write regular expressions in C

Educative Answers Team

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.

svg viewer

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.

Creating a regular expression

The regcomp() function is used to compile a regular expression. According to the official documentation, it takes three arguments:

  1. A pointer to the memory location where the pattern to be matched is stored.
  2. A pointer to a pattern of the string type.
  3. A flag to specify the type of compilation.

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(&regex,"[:lower:]",0);
    if (return_value == 0){
        printf("Regular expression compiled successfully.");
    }
    else{
        printf("Compilation error.");
    }
    return 0;
}

Matching a pattern

The regexec() function is used to match a string against a pattern. According to the official documentation, it takes in five arguments:

  1. A precompiled pattern
  2. A string in which the pattern needs to be searched for.
  3. Information regarding the location of matches.
  4. Information regarding the location of matches.
  5. Flags to specify a change in the matching behavior.

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(&regex,"ice",0);
  return_value = regexec(&regex, "icecream", 0, NULL, 0);
  return_value2 = regcomp(&regex,"ice",0);
  return_value2 = regexec(&regex, "frozen yoghurt", 0, NULL, 0);
  print_result(return_value);
  print_result(return_value2);
  return 0;
}

Using a regular expression with 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

c
regex
pattern matching
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring