c++ - regular expression

Hi,

I need help on using regular expressions with c++. I am willing to get digit/integer value after the character "Off". The input is like this:

Off 9

So, I need value 9.

Please guide me.

Thanks
josephm67Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
You may want to keep this for reference: http://cpprocks.com/wp-content/uploads/c++11-regex-cheatsheet.pdf

HTH,
Dan
0
 
käµfm³d 👽Connect With a Mentor Commented:
You haven't specified which library you are using, so the syntax may vary, but you should be able to get it with:

Off ([0-9]+)

Open in new window


Then just inspect the value of the first capture group.
0
 
peprConnect With a Mentor Commented:
As Dan wrote, if you  can choose the compiler, choose the one that conforms with the latest C++11 standard where regexes are implemented in the standard library. For MS VS it is Visual Studio 2013. Otherwise, you may want to focus on the Boost regexes that are very similar with the new standard library. However, you may have to learn to compile the Boost first, which may be time consuming for a beginner. Then it could be easier to extract  the value via some simpler parsing.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
sarabandeConnect With a Mentor Commented:
if you want to check a string if it contains "Off9" you don't need regular expressions,

std::string s = "Any String contents containing Off9 somewhere in it";
if (s.find("Off9", 0) != std::string::npos)
{
        // string found

Open in new window


the string::find function would return a position within the string on success and the second argument is the position to begin with the search. you also could use member functions like find_first_of to check for proceeding characters or digits:

std::string s = "Any String contents containing Off and digit 0,1,2,..9 after in it";
size_t pos1, pos2;
if ((pos1 = s.find("Off", 0)) != std::string::npos)
{
       pos1 += strlen("Off");
       pos2 = s.find_first_of("0123456789", pos1);
       if (pos2 != string::npos)
       {
            //  found

Open in new window


of course regex looks more professional than parsing with find functions but it also requires more skill and therefore it is more error-prone.

Sara
0
 
käµfm³d 👽Commented:
and therefore it is more error-prone
I would argue to the contrary, at least in this scenario.
0
 
sarabandeConnect With a Mentor Commented:
it is very difficult to define a non-trivial regular expression where you can prove that its results are valid for all cases of inputs. probably it is impossible if you don't have full control on the texts. that is what i call error-prone.

I have mixed experiences with regex. for example we used regular expressions in a database which worked as a user-defined filter on description fields. nearly every day, we had to support Users because the regular expressions didn't do what they expected. in another case I used them with success to parse huge log files for some specific messages.

a self-written parser has a finite number of cases where each branch could be fully tested. in my opinion the complexity is incomparably smaller than with using a "blackbox" like regex and therefore less error-prone.

... at least in this scenario
do you really think regex is the right means to search for "Off?9" in a text?

Sara
0
 
käµfm³d 👽Commented:
Regex is all about matching patterns. People get into trouble with regex when they think regex is designed to do more than that. Do I think that regex is the right means? It's not the wrong way, IMO. As you know, in programming there are many different ways to accomplish the same goal. Personally, having spent a great deal of time learning and working with regex, I find your code to be overly "wordy" for the task at hand. That's not meant to be an attack; it's just how I view it. I find nothing technically wrong with your approach, so the observation is merely stylistic in nature.
0
 
sarabandeConnect With a Mentor Commented:
be overly "wordy"

std::string s = "Any String contents containing Off and digit 0,1,2,..9 after in it";
size_t pos1, pos2, nil = std::string::npos;
if ((pos1 = s.find("Off")) !=  nil && (pos2 = s.find_first_of("0123456789", pos1+3))  != nil)
{
       //  found 

Open in new window


better?

Sara
0
 
josephm67Author Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.