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

________ give more information on how the data is stored, entered, and processed

A) Field names B) Description C) Data types D) Field properties

Computer Science & Information Technology

Specific information in a database can be located by using the ________ tool

A) Search B) Locate C) Find D) Browse

Computer Science & Information Technology

To record a macro, a worksheet must be protected

Indicate whether the statement is true or false

Computer Science & Information Technology

JavaScript is a valuable tool, but can also be used to write and distribute viruses. Name three features of JavaScript that allow it to be easily used for ill will.

What will be an ideal response?

Computer Science & Information Technology