Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c++ - regular expression

Posted on 2014-03-24
9
Medium Priority
?
375 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 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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

782 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