Regular Expression in C


Submit solution

Points: 0
Time limit: 0.5s
Memory limit: 20K

Author:
Problem type
Allowed languages
C

Regex in C

A regular expression is a sequence of characters that forms a search pattern. It consists of metacharacters, literals, and special characters that define the pattern we want to match. Here are some common elements used in regular expressions:

  • Metacharacters:

    • . (dot): Matches any single character except a newline.
    • * (asterisk): Matches zero or more occurrences of the preceding character or group.
    • + (plus): Matches one or more occurrences of the preceding character or group.
    • ? (question mark): Matches zero or one occurrence of the preceding character or group.
    • | (vertical bar): Acts as an OR operator, allowing multiple alternatives in a pattern.
  • Character classes:

    • [ ] (brackets): Matches any single character within the brackets.
    • [^ ] (caret inside brackets): Matches any single character not within the brackets.
    • - (hyphen inside brackets): Represents a range of characters within brackets.
  • Anchors:

    • ^ (caret): Matches the start of a line or string.
    • $ (dollar sign): Matches the end of a line or string.
  • Quantifiers:

    • {n}: Matches exactly n occurrences of the preceding character or group.
    • {n,}: Matches n or more occurrences of the preceding character or group.
    • {n,m}: Matches between n and m occurrences of the preceding character or group.
  • Escape sequences:

    • \: Escapes a metacharacter, treating it as a literal character.
    • \d: Matches any digit (equivalent to [0-9]).
    • \w: Matches any word character (letters, digits, or underscores).
    • \s: Matches any whitespace character (spaces, tabs, newlines).
Example

Below program demonstrates the validation of Email and Username using Regular Experessoin (RegEx) with the help of regex.h

#include <stdio.h>
#include <regex.h>

/**
 * Validates an email address using regular expressions.
 * Returns 1 if the email is valid, 0 otherwise.
 */
int validateEmail(const char *email) {
    // Regular expression pattern for email validation
    const char *pattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";

    regex_t regex;
    int reti;

    // Compile the regular expression pattern
    reti = regcomp(&regex, pattern, REG_EXTENDED);
    if (reti) {
        printf("Email regex compilation failed\n");
        return 0;
    }

    // Execute the regular expression against the email
    reti = regexec(&regex, email, 0, NULL, 0);
    regfree(&regex);

    if (!reti) {
        printf("%s is a valid email address\n", email);
        return 1;
    } else if (reti == REG_NOMATCH) {
        printf("%s is not a valid email address\n", email);
        return 0;
    } else {
        printf("Email regex matching failed\n");
        return 0;
    }
}

/**
 * Validates a username using regular expressions.
 * Returns 1 if the username is valid, 0 otherwise.
 */
int validateUsername(const char *username) {
    // Regular expression pattern for username validation
    const char *pattern = "^[a-zA-Z0-9._-]{3,16}$";

    regex_t regex;
    int reti;

    // Compile the regular expression pattern
    reti = regcomp(&regex, pattern, REG_EXTENDED);
    if (reti) {
        printf("Username regex compilation failed\n");
        return 0;
    }

    // Execute the regular expression against the username
    reti = regexec(&regex, username, 0, NULL, 0);
    regfree(&regex);

    if (!reti) {
        printf("%s is a valid username\n", username);
        return 1;
    } else if (reti == REG_NOMATCH) {
        printf("%s is not a valid username\n", username);
        return 0;
    } else {
        printf("Username regex matching failed\n");
        return 0;
    }
}

int main() {
    // Valid email and username
    const char *vemail = "cb.en.p2cys17018@cb.students.amrita.edu";
    const char *vusername = "r_ramaguru";

    // Invalid email and username
    const char *ivemail = "cb.en.p2cys17018@@cb.students.amrita.edu";
    const char *ivusername = "18_ramaguru";

    // Validate valid email and username
    validateEmail(vemail);
    validateUsername(vusername);

    printf("\n");

    // Validate invalid email and username
    validateEmail(ivemail);
    validateUsername(ivusername);

    return 0;
}

Comments

There are no comments at the moment.