Solved

c++ - regular expression

Posted on 2014-03-24
9
353 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
9 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 84 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 34

Accepted Solution

by:
Dan Craciun earned 84 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 28

Assisted Solution

by:pepr
pepr earned 83 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
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 249 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 74

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 32

Assisted Solution

by:sarabande
sarabande earned 249 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 74

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 32

Assisted Solution

by:sarabande
sarabande earned 249 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now