Well, that regex would be in two parts, the (optional) whole number:
(:?-?\d+ )?
and the fractional part:
-?\d+/-?\d+
And we need to match the complete string; so:
^(:?-?\d+ )?-?\d+/-?\d+$
Testing a bit:
PS> $re=[regex]'^(:?-?\d+ )?-?\d+/-?\d+$'
PS> "1/2","12 1/2","-12 2/3","-5/8","5/-8"|%{$_ -match $re} | gu
True
However, this allows for "-12 -2/-3"
as well, or things like "1 -1/2"
which don't make much sense.
ETA: Your original regex works, too. It just lacked the anchors for begin and end of the string (^
and $
, respectively). Adding those make it work correctly.