0

I'm just trying to mess around and get familiar with using regex in c++. Let's say I want the user to input the following: ###-$$-###, make #=any number between 0-9 and $=any number between 0-5. This is my idea for accomplishing this:

regex rx("[0-9][0-9][0-9]""\\-""[0-5][0-5]")

That's not the exact code however that's the general idea to check whether or not the user's input is a valid string of numbers. However, let's say i won't allow numbers starting with a 0 so: 099-55-999 is not acceptable. How can I check something like that and output invalid? Thanks

Richard
  • 5,840
  • 36
  • 123
  • 208
  • 2
    I am unsure what is being asked? Could you try and clarify your question Richard? – hmjd Dec 02 '11 at 21:47
  • What make #=... means? You need to provide input/output. – FailedDev Dec 02 '11 at 21:50
  • Related: http://stackoverflow.com/questions/457595/can-you-use-boost-regex-to-parse-a-stream – AJG85 Dec 02 '11 at 21:54
  • 1
    I noticed you tagged with VS2010. Have you considered using std::regex? It's part of the standard library and I think it's based off boost's implementation. If you are bringing in boost dependencies in your project only for regex, you might want to drop boost in favor of the standard headers. – fronsacqc Dec 02 '11 at 21:57

3 Answers3

4
[0-9]{3}-[0-5]{2}-[0-9]{3}

matches a string that starts with three digits between 0 and 9, followed by a dash, followed by two digits between 0 and 5, followed by a dash, followed by three digits between 0 and 9.

Is that what you're looking for? This is very basic regex stuff. I suggest you look at a good tutorial.

EDIT: (after you changed your question):

[1-9][0-9]{2}-[0-5]{2}-[0-9]{3}

would match the same as above except for not allowing a 0 as the first character.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1
std::tr1::regex rx("[0-9]{3}-[0-5]{2}-[0-9]{3}");

Your talking about using tr1 regex in c++ right and not the managed c++? If so, go here where it explains this stuff.

Also, you should know that if your using VS2010 that you don't need the boost library anymore for regex.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • He's talking about `boost::regex` which the standard regex was based on I do believe. – AJG85 Dec 02 '11 at 21:58
  • @AJG85 No he posted a similar question a while ago. He is on MSVS 2010. – FailedDev Dec 02 '11 at 21:59
  • Right, its *based on* boost::regex, but because tr1 will become part of the standard library sometime in the future, in VS2010 they have implemented all tr1 stuff under the tr1 namespace since its not actually part of the standard yet. – Jesse Good Dec 02 '11 at 22:01
  • @FailedDev: Well then he's either unaware of `std::regex` or it's incorrectly tagged with boost-regex. – AJG85 Dec 02 '11 at 22:01
1

Try this:

#include <regex>
#include <iostream>
#include <string>

int main()
{
    std::tr1::regex rx("\\d{3}-[0-5]{2}-\\d{3}");
    std::string s;
    std::getline(std::cin,s);
    if(regex_match(s.begin(),s.end(),rx))
    {
        std::cout << "Matched!" << std::endl;
    }
}

For explanation check @Tim's answer. Do note the double \ for the digit metacharacter.

FailedDev
  • 26,680
  • 9
  • 53
  • 73