Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for blood pressure

I have the following regular expression to validate blood pressure values in the form of systolic/diastolic:

\b[0-9]{1,3}\/[0-9]{1,3}\b

The expression works with the only flaw that it allows more than one non-consecutive slash (/). For example, it allows this 2/2/2. I want it to allow only the format of a number from 1 to 999, and slash, and again a number from 1 to 999. For example, 83/23, 1/123, 999/999, 110/80, etc. Can anybody give me some help with this?

The only other expression I've found is here: ^\b(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)\\/(29[0-9]|2[0-9][0-9]|[01]?[0-9][0-9]?)$, but it doesn't work.

BTW, I'm using jquery.

Thanks.

like image 394
Cesar Vinas Avatar asked Feb 16 '13 22:02

Cesar Vinas


2 Answers

Use ^ and $ to match the beginning and end of the string:

^\d{1,3}\/\d{1,3}$

By doing so, you force the matched strings to be exactly of that form.

like image 164
Blender Avatar answered Sep 23 '22 15:09

Blender


Don't use the \b word-boundaries because a slash counts as a word boundary.

The use of ^ and/or $ is likely your most simple solution. Unfortunately, if your input is a part of a string or sentence or occurs more than once in a line, etc., you've got more thinking to do.

like image 28
Steven Lu Avatar answered Sep 21 '22 15:09

Steven Lu