Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c++ - regular expression

Posted on 2014-03-24
9
Medium Priority
?
370 Views
Last Modified: 2014-04-04
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
0
Comment
Question by:josephm67
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 336 total points
ID: 39951123
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
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 336 total points
ID: 39951152
You may want to keep this for reference: http://cpprocks.com/wp-content/uploads/c++11-regex-cheatsheet.pdf

HTH,
Dan
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 332 total points
ID: 39952450
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 996 total points
ID: 39954081
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39954102
and therefore it is more error-prone
I would argue to the contrary, at least in this scenario.
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 996 total points
ID: 39954275
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39954306
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 996 total points
ID: 39955235
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
 

Author Closing Comment

by:josephm67
ID: 39978272
thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question