I want to validate a user input against format specifier.For example format specifiers %4.5f , %4f , %10.5f, etc., If i specify %5.2f as format specifier I should able to validate user input whether its satisfies the given format %5.2f or not?
Note : I am using C++ in windows environment.
You will want to do string matching against a regular expression. The expression should be:
Here n and m are the number of digits to want. You can build a regular expression from a string, so just make the pattern string from your parameters and then construct the regex; it’s all in
<regex>on C++0x compilers (or<tr1/regex>otherwise).Be careful to either match the entire input string or surround the expression by word boundaries to avoid 12345.67890 matching against m=2, n=2 as “45.67”.
Edit: Here’s one approach: