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

4
  • 2
    I am unsure what is being asked? Could you try and clarify your question Richard? Commented Dec 2, 2011 at 21:47
  • What make #=... means? You need to provide input/output. Commented Dec 2, 2011 at 21:50
  • Related: stackoverflow.com/questions/457595/… Commented Dec 2, 2011 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. Commented Dec 2, 2011 at 21:57

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This was almost my exact answer about to be posted :(
The guy answers in less than 1 minute max. :) If he sleeps we may answer too :)
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.

4 Comments

He's talking about boost::regex which the standard regex was based on I do believe.
@AJG85 No he posted a similar question a while ago. He is on MSVS 2010.
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.
@FailedDev: Well then he's either unaware of std::regex or it's incorrectly tagged with boost-regex.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.