A few answers here seem overly complex. Instead, this simple regular expression should do this: [0-3]\.\d|4\.0 , assuming that the one-bit form (for example, "1") is invalid. (We do not use the GPA from where I come from, so I donβt know if this is a safe guess.)
With anchors in front and behind, as I see others here use:
^([0-3]\.\d|4\.0)$
Or if you do not need a capture group:
^(?:[0-3]\.\d|4\.0)$
Full explanation: the regular expression matches either 0, 1, 2, or 3, followed by a period and any single digit or literal string 4.0.
source share