Solved

c++ - regular expression

Posted on 2014-03-24
9
359 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 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 35

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 29

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

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
 
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 34

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 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 34

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to gracefully close the c++ 11 thread? 3 160
sorting efficency of sorting algorithm 30 151
Gaming Software 1 39
How do I get Window Title of all opened process? 4 61
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

752 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