How to validate Roman Numerals using Regular Expression in C++

Hello friends, today we’ll be learning to validate Roman numerals using regular expressions. Firstly, the user will enter a string which and we will validate. Finally, we will return whether the entered string is a valid Roman numeral or not.

To use regular expressions in C++, we use the ‘regex’ header.

Validating Roman Numerals using Regular Expressions

The regular expression used for checking the Roman numerals is ^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$. Here, the caret symbol (^) matches the beginning of the entered string.  Similarly, the dollar sign ($) matches the end of the entered string.

The regular expression is further divided into four-character groups:

    • “M{0,3}”
    • “(CM|CD|D?C{0,3})”
    • “(XC|XL|L?X{0,3})”
    • “(IX|IV|V?I{0,3})”

The four-character groups are, in fact, similar in their functions.

The fourth character group (IX|IV|V?I{0,3}) checks for the numbers lying in 0-9. It can be further divided into “IX|IV|V?” and “I{0,3}“. The second character group matches “I” for 0 or 1 or 2 or 3 times, while the initial character group matches “IX”, or “IV” or “V” for 0 or 1 times.

The first three character groups “M{0,3}", "(CM|CD|D?C{0,3})", "(XC|XL|L?X{0,3})” checks for numbers lying between 3000-3999, 100-999, and 10-99 respectively.

For matching the regular expression with the entered string, we use “regex_match” which returns boolean values.

Code:

#include <iostream>
#include <regex>

bool isValid(std::string data) {
    std::regex reg("^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$");

    return std::regex_match(data, reg);
}

int main() {
    std::string input;
    std::cout << "Enter the data: ";
    std::cin >> input;

    if (isValid(input))
        std::cout << "The entered data is a valid Roman numerical." << std::endl;
    else
        std::cout << "The entered data is not a valid Roman numerical." << std::endl;


    return 0;
}

Output:

C:\Users\User\Desktop>validator.exe
Enter the data: XCIX
The entered data is a valid Roman numerical.

Here, if you run the program you can see it will only work for Capital roman numerals. If you wish these to work for the lowercase or small letter roman numerals too then you can convert the given string to uppercase.

Leave a Reply

Your email address will not be published. Required fields are marked *