Please help me with the regular expression itself. I am learning it. I don’t need replacement code.
I am working on wxWidget 2.8.12 with VS2008 (C++). I’d like to check whether TEST_STRING is float number. (-0.1, 0.1, 1 etc.). I think the expression itself is correct, I checked by tools. Do I make mistake with wxWidget?
wxString tmpStr = TEST_STRING;
wxRegEx reNegativeFloatNum(_("^[-]?[0-9]*\\.?[0-9]+$"));
bool tmp = reNegativeFloatNum.Compile(tmpStr);
tmp = tmp && reNegativeFloatNum.IsValid();
if ( tmp && reNegativeFloatNum.Matches(tmpStr))
{
//Do something
}
else
{
//Do something else
}
My real question is why the regular expression doesn’t work? If I input ‘a’-‘Z’, the Matches() return ‘true’. Any one know? I just want to learn the regular expression.
As the OP really wants to use regexes, I looked at it in details and the original regex works just fine to me. Notice that you don’t need to use
_()around regex string as this is not something you’d ever need to translate (_is a short synonym forwxTRANSLATE()) and thatCompile()is already called by the ctor. So here is a shorter version:And it works as expected: