0

let's say i hve a string S = "1 this is a number=200; Val+54 4class find57"

i want to use Regex to extract only this numbers:

   num[1] = 1
   num[2] = 200
   num[3] = 54

and not the 4 in "4class" or 57 in "find57" which means only numbers that are surrounded by Operators or space. i tried this code but no results:

std::string str = "1 this is a number=200; Val+54 4class find57";
 boost::regex re("(\\s|\\-|\\*|\\+|\\/|\\=|\\;|\n|$)([0-9]+)(\\s|\\-|\\*|\\+|\\/|\\;|\n|$)");
 boost::sregex_iterator m1(str.begin(), str.end(), re);
 boost::sregex_iterator m2;
 for (; m1 != m2; ++m1) {

    advm1->Lines->Append((*m1)[1].str().c_str());

                        } 

by the way i'am using c++ Builder XE6.

1 Answer 1

3

Just use word boundaries. \b matches between a word character and a non-word character.

\b\d+\b

OR

\b[0-9]+\b

DEMO

Escape the backslash one more time if necessary like \\b\\d+\\b or \\b[0-9]+\\b

Sign up to request clarification or add additional context in comments.

2 Comments

how would you also support floating point numbers ?
\b\d+(?:\.\d+)?\b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.