I have a string that goes like “-23.45m / abc = 53.02m/s” that I want to take apart. You’d think there was an easy way in .net to get the -23.45 (like using a built-in float interpreter) and tell me that the rest of the string starts at ‘m’.
In C++ I would use
double num;
wchar_t* input_text = L"-23.45m / abc = 53.02m/s";
if (swscanf(input_text, L"%lf%n", &num, &count) == EOF)
return false;
pos += count;
You could use a regular expression to match the double value at the start. You’ll need to consider what you want to support. For example, do you want to support all of these:
You should also consider what culture you want to parse in – “1,000” can mean “one thousand” or “one point zero zero zero” depending on your culture.
Once you’ve got your specifications nailed down, writing a regex shouldn’t be too hard. That will let you match the double value in one capture and the rest of the string in another.