# Validate a string using regular expressions - 1-30 with up to 2 decimal places

As the title suggests I need a regular expression that can validate an input string to make sure it is a number between 1-30 and up to 2 decimal places.

For instance,

4 is fine
10.25 is fine
15.3 is fine
29.99 is fine
30 is fine
30.01 is not fine

EDIT: so it has to be a regular expression due to the limitations of the system I'm using, I have tried several things,

I can get as far as

^\d{1,2}(\.\d{1,2})?\$

which allows up to a 2 digit number with up to 2 decimal places.

EDIT: For your specific situation of needing a regex, try this:

^(?!0)(30(\.0{1,2})?|[12]?\d(\.\d{1,2})?)\$

Breakdown:

^ ... \$ - To make sure the regex starts and ends at the start and end of the string.

(30(\.0{1,2})? - 30 optionally followed by .0 or .00

| Or...

[12]? Tens digit of 1 or 2 only

\d One non-optional units digit

(\.\d{1,2})? Optional .digit or .digitdigit

Unfortunately, this regex is not easily tunable to fit any range of numbers. (It would be an interesting project to write a program to automatically spit out regexes like this one.)

Otherwise I would say: Don't re-invent the wheel.

double result;
if (double.TryParse(inputstring, out result))
{
if (result >= 1.0 & result <= 30.0)
{
return true;
}
}
return false;

If it's not two decimal places and you need it to be, you can calculate Round(result, 2) so it is.

### Displaying rows from sqlite databse using QTableWidget in PyQt4

with sqlite3.connect('database.db') as db:

### Google Calendar crashes because I added an event with endTime earlier than startTime

So I added an event to my Google Calendar using the CalendarProvider according to this example.