I think this works pretty good. It uses a regular expression to validate a double. It most likely does NOT illustrate best practices for regular expression use in C.
Code:
#include <stdio.h>
#include <regex.h>
int main (int argc, const char * argv[]) {
char data[80] ;
int rc ;
regex_t * myregex ;
regmatch_t arrayOfMatches[2];
// Compile the regular expression
rc = regcomp( myregex,
"^ *[-+]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][-+]?[0-9]+)? *$",
REG_EXTENDED | REG_NOSUB ) ;
printf("RC from regcomp() = %d\n", rc);
printf("Enter a double value\n");
scanf("%s",data) ;
// Compare the entered value to the regex
if (!regexec(myregex, data, 1 , arrayOfMatches, 0 ) ) {
printf("double %s is valid.\n", data) ;
}
else printf("double %s is not valid\n", data );
return 0;
}
Todd