Write a regular expression that will search a string and match a valid number. A number can have any number of digits, but it can have only digits and a decimal point. The decimal point is optional, but if it appears in the number, there must be only one, and it must have digits on its left and its right. There should be whitespace or a beginning- or end-of- line character on either side of a

valid number. Negative numbers are preceded by a minus sign.

What will be an ideal response?

```
#include
#include
#include
using namespace std;

int main()
{
string sentence;
boost::smatch match;

// regular expression to match a number
boost::regex expression( "(\\s|^)-?\\d+(\\.\\d+)?(\\s|$)" );
cout << "Enter a string with a number: ";
getline( cin, sentence ); // get a string from the user

// check if the sentence contained any numbers
if ( !( boost::regex_search( sentence, expression ) ) )
cout << "No numbers found in the string." << endl;
else
{
cout << "\nNumber(s) found in the string:\n";

// match regular expression to string and print out all matches
while ( boost::regex_search( sentence, match, expression) )
{
cout << match << endl; // print the matching string

// remove the matched substring from the string
sentence = match.suffix();
} // end while
} // end else
} // end main
```
Enter a string with a number: -1234.56 987.54 123.45.6
Number(s) found in the string:
-1234.56
987.54

Computer Science & Information Technology

You might also like to view...

Which approach to stateful protocol analysis involves detection of the protocol in use, followed by activation of analyzers that can identify applications not using standard ports?

A. Protocol state tracking B. IP packet reassembly C. Traffic rate monitoring D. Dynamic Application layer protocol analysis

Computer Science & Information Technology

Which openSUSE administrator tool enables administrators to install and manage software?

A. YaST B. Setup C. Ysetup D. SetMan

Computer Science & Information Technology