Search code examples
c++regexvisual-studio-2012fractions

Regex Validation for Mixed Fractions - Visual C++ 2012


I'm new to RegEx, working in Visual Studio (C++) and need to check to see if an input is a valid mixed fraction.

So far what I have works for all cases, except I want to make sure that the denominator can't be 0 (eg. 2 3/0 ).

Here's the RegEx I'm currently using:

regex mixedFraction("(-?[[:digit:]]+)[\\s]([[:digit:]]+)[/]([[:digit:]]+)")

I've tried variations, but can't seem to figure out something that works. In essence, I really just need it to check that only the first digit of the denominator isn't 0 -- it will be fine for it to fail validation if someone were to enter '2 3/01'

Thanks for any and all help


Solution

  • This should work for you.

    regex mixedFraction("(-?[0-9]*)\\s([0-9]*)/([1-9][0-9]*)")