1

I have a string of this format that I need to verify:

H12345-001

Here the first character should be an alphabetical character followed by 5 digits, then a dash (-), then two zeroes and finally a digit.

I am not sure if regex is right choice or can do it in a naive way of comparing each character. If regex is right way to go can someone point out to an example tutorials to use it. I am using C++03 (i.e. no C++11).

Rapptz
  • 20,807
  • 5
  • 72
  • 86
  • There's [Boost.Regex](http://www.boost.org/doc/libs/1_57_0/libs/regex/doc/html/index.html). Or [Poco's Regex](http://www.appinf.com/docs/poco/Poco.RegularExpression.html). But this is so simple I wouldn't bring in a whole new library just for this (if you're already using a library that provides regex, sure, use it). Otherwise, I'd just verify this with a custom function. – Cornstalks Dec 04 '14 at 21:30
  • We are not using Boost in our proj. Thats why I have to decide if I have to bring in boost to validate our string or can just check char by character. Just wanna seek expert advise if I'm thinking right – Sri Krishna Mallikarjuna Dec 04 '14 at 21:35
  • "C++ 3.0"? Do you mean Borland Turbo C++ 3.0? If so, that's positively antique. – nobody Dec 04 '14 at 21:35

4 Answers4

1

Assuming your string can include a lowercase letter, the regex is [a-zA-z]\d\d\d\d\d-00\d (just remove the a-z if you don't want lowercase letters).

Simple custom validator if bringing in a regex library isn't worth it (demo):

bool isValid(const std::string& str)
{
    return
        (str.size() == 10) &&
        (('a' <= str[0] && str[0] <= 'z') ||
        ('A' <= str[0] && str[0] <= 'Z')) &&
        ('0' <= str[1] && str[1] <= '9') &&
        ('0' <= str[2] && str[2] <= '9') &&
        ('0' <= str[3] && str[3] <= '9') &&
        ('0' <= str[4] && str[4] <= '9') &&
        ('0' <= str[5] && str[5] <= '9') &&
        (str[6] == '-' && str[7] == '0' && str[8] == '0') &&
        ('0' <= str[9] && str[9] <= '9');
}
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • I think there are `isalpha()` and `isdigit()` functions for such purpose – Slava Dec 04 '14 at 21:44
  • Yup. They're provided under `` or `` for the locale aware versions. – Rapptz Dec 04 '14 at 21:47
  • @Slava: Yes, but those rely on the current locale. I haven't given enough thought to locales in this answer to suggest those two functions, so I've ignored them. – Cornstalks Dec 04 '14 at 21:48
1

You can use Boost.Regex for this:

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>

boost::regex your_regex("[A-Z]\\d{5}-00\\d");
boost::smatch match;

int main() {
    std::string input = "H12345-001";
    std::string output;
    if(boost::regex_match(input, match, your_regex)) {
        output = boost::lexical_cast<std::string>(match);
        std::cout << output << std::endl;
    } 
    return 0;
}

This is an example for matching a single occurrence, you will need to adjust it to your needs.

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
syntagma
  • 23,346
  • 16
  • 78
  • 134
0

Any regex library will do. There's pcre for POISX systems, for example, where you just #include <regex.h> and call the regcomp family of functions ( man 3 regcomp for more info). Nothing in those should require C++11.

Frankly, if you're going with regexes, which for this problem doesn't seem to be worth it, the one difficult issue will be to have the regex pattern proper in the C++ code, because of having to escape all the backslashes and stuff. You might want to try this awesome solution here in SO to work with the regexes in C++<11 without much problem.

Otherwise, go with Cornstalk's answer.

Community
  • 1
  • 1
Luis Machuca
  • 1,047
  • 9
  • 16
-1

This regex is pretty bad but the easiest to understand that I could come up with that will match what you want.

([A-Z])\d\d\d\d\d-00\d

Here is a tutorial on it like requested: http://en.cppreference.com/w/cpp/regex

marsh
  • 2,592
  • 5
  • 29
  • 53