Iam looking for Regex which can accept values between 1 and 250 .The Following values should not be accepted ("'!##$)$()(_+) and -1-2-3-4 and so on
/^[1-9][0-9][0-9]?$/
/^250|(?:2[0-4]|1[0-9])[0-9]|[1-9][0-9]?$/
This can easily be done using numeric comparison operators.
But if you want a regex for it here you go:
^(?:250|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Note that this allows leading zeros, if you don't want them you can see Ignacio's answer.
Related
I am looking for a regular expression which allows only the time offset values.
I have used:
^(?:[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])$
The ONLY strings I need to match:
-12:00
+14:00
-11:00
-10:00
-09:30
-09:00
-08:00
-07:00
-06:00
-05:00
-04:00
-03:30
-03:00
-02:00
-01:00
00:00
+01:00
+04:00
+03:30
+03:00
+02:00
+04:30
+05:00
+05:30
+05:45
+06:00
+06:30
+07:00
+08:00
+08:45
+09:00
+09:30
+10:00
+10:30
+11:00
+12:00
+12:45
+13:00
+14:00
Please check here for what I have tried so far, and the values I want it to allow.
It works fine for the all the values except for 00:00.
Also, it allows some extra values such as -19:30 +23:00 22:30 21:00 which should not be allowed.
I want it to allow only those values which have been mentioned in my aforesaid link.
I was able to achieve the results you wanted by slightly tweaking your regex.
This is also short and concise.
^(?:(?:[+-](?:1[0-4]|0[1-9]):[0-5][0-9])|00:00)$
You can check the results and test it further here
One point which should be noted here is that you would be able to pass other values between the current valid values of timezone(-12:00 to +14:00). By reading the comments in the question, I feel it is better to have it this way, for future proofing just in case they change. (You would need to tweak the regex to allow values greater than 14:00)
If you strictly want to limit it to the values which you have listed, enumeration would be a better approach to go about it.
To only match these strings of yours, you can use
^(?:\+(?:0(?:[12]:00|[34]:[03]0|5:(?:[03]0|45)|6:[03]0|7:00|8:(?:00|45)|9:[03]0)|1(?:0:[03]0|1:00|2:(?:00|45)|[34]:00))|-(?:0(?:[12]:00|3:[03]0|[4-8]:00|9:[03]0)|1[0-2]:00)|00:00)$
See the regex demo
Use online/external tools to build word list regexps like this (e.g. My Regex Tester, etc.).
The following will give me 9090 but I wish to get -9090
regexp_replace('abcd-9090',[^0-9],'')
If I use regexp_replace('abcd-9090',[^0-9-],'')
then it gives -9090
but when the string is abcd9090- it would give me 9090-
There could be many more cases I guess where abc-abcd-9090 would give me -9090 but its safe to assume that such will not be the case and there would be only a single - before the numeric values.
Since there could be many cases , I am just supposed to assume the best and replace the flawed code with a more correct pattern which produces an integer almost always.
May be like assuming a condition where only single - could come at the beginning of any digits in the string is okay to assume.
Any help is appreciated.
I guess you can try to use regexp_extract instead:
regexp_extract('abcd-9090','.*(-[0-9]+)',1)
UPD from comment - author need to address one more corner case:
regexp_extract(regexp_replace('-ab2cd9090','[^\\d-]+',''),'(-?\\d+)',1)
I am working under the Web Application based on ASP.NET MVC 5 and I have a great problem in my project with the field which gives the user the ability to choose format for showing Dates in the application.
The goal is to make RegularExpressionAttribute with the regex for validation date formats inputted by user.
Acceptable formats must be:
m/d/y,
m-d-y,
m:d:y,
d/m/y,
d-m-y,
d:m:y,
y/m/d,
y-m-d,
y:m:d
and the length of the date symbols may be as 'y' so far 'yyyy'. And they can be upper case.
So after hard-coding I've made the acceptable one:
((([mM]{1,4})([\/]{1})([dD]{1,4})([\/]{1})([yY]{1,4}))|(([mM]{1,4})([\-]{1})([dD]{1,4})([\-]{1})([yY]{1,4}))|(([mM]{1,4})([\:]{1})([dD]{1,4})([\:]{1})([yY]{1,4})))|((([dD]{1,4})([\/]{1})([mM]{1,4})([\/]{1})([yY]{1,4}))|(([dD]{1,4})([\-]{1})([mM]{1,4})([\-]{1})([yY]{1,4}))|(([dD]{1,4})([\:]{1})([mM]{1,4})([\:]{1})([yY]{1,4})))|((([yY]{1,4})([\/]{1})([mM]{1,4})([\/]{1})([dD]{1,4}))|(([yY]{1,4})([\-]{1})([mM]{1,4})([\-]{1})([dD]{1,4}))|(([yY]{1,4})([\:]{1})([mM]{1,4})([\:]{1})([dD]{1,4})))|((([yY]{1,4})([\/]{1})([dD]{1,4})([\/]{1})([mM]{1,4}))|(([yY]{1,4})([\-]{1})([dD]{1,4})([\-]{1})([mM]{1,4}))|(([yY]{1,4})([\:]{1})([dD]{1,4})([\:]{1})([mM]{1,4})))
This one works... But according to my scarce regex knowledge and experience I hope to get some help and better example for resolving this puzzle.
Thanks.
You have to generalize a bit.
m{1,4}([:/-])d{1,4}\1y{1,4}|d{1,4}([:/-])m{1,4}\2y{1,4}|y{1,4}([:/-])m{1,4}\3d{1,4}
Explanation:
instead of e.g. [mM] use m and set option for case insensitive match
([:/-]) all allowed delimiters as group
\1...\3 back reference to the delimiter group 1...3
I'm currently using ([1-9]|1[0-2]) to represent inputs from 1 to 12. (Leading zeros not allowed.)
However it seems rather hacky, and on some days it looks outright dirty.
☞ Is there a proper in-built way to do it?
☞ What are some other ways to represent number ranges?
I tend to go with forms like [2-9]|1[0-2]? which avoids backtracking, though it makes little difference here. I've been conditioned by XML Schema to avoid such "ambiguities", even though regex can handle them fine.
Yes, the correct one:
[1-9]|1[0-2]
Otherwise you don't get the 10.
Here is the better answer, with exact match from 1 - 12.
(^0?[1-9]$)|(^1[0-2]$)
Previous answers doesn't really work well with HTML input regex validation, where some values like '1111' or '1212' will still treat it as a valid input.
You can use:
[1-9]|1[012]
How about:
^[1-9]|10|11|12$
Matches 0-9 or 10 or 11 or 12. thats it, nothing else is matched.
You can try this:
^[1-9]$|^[1][0-2]$
Use the following pattern (0?[1-9]|1[0-2]) use this which will return values from 1 to 12 (January to December) even if it initially starts with 0 (01, 02, 03, ..., 09, 10, 11, 12)
The correct patter to validate numbers from 1 to 12 is the following:
(^[1-9][0-2]$)|(^[1-9]$)
The above expression is useful when you have an input with type number and you need to validate month, for example. This is because the input type number ignores the 0 in front of any number, eg: 01 it returns 1.
You can see it in action here: https://regexr.com/5hk0s
if you need to validate string numbers, I mean, when you use an input with type text but you expect numbers, eg: expiration card month, or months the below expression can be useful for you:
((^0[1-9]$)|(^1[0-2]$))
You can see it in action here https://regexr.com/5hkae
I hope this helps a lot because it is very tricky.
Regards.
In python this matches any number between 1 - 12:
12|11|10|9|8|7|6|5|4|3|2|1
The descending order matters. In ascending order 10, 11 and 12 would match 1 instead as regex usually pick the first matching value.
I would like to write a regular expression to validate and input field against the following arguments:
field is required (cannot be
empty)
field must not be a negative number
field must be a validate decimal
number to two decimals (eg. 1 or 1.3
or 1.23)
field can be any valid number between 0 and 100 or an 'e'
Regular expressions find great use in checking format, but you're wishing to use it to do a subset of floating point number parsing and bounds checking. Be kind to yourself and the person who will maintain your code after you're gone: check if it's an 'e', else read it into a float and check the bounds.
You can use: ^(100|\d{1,2}(\.\d{1,2})?|e)$
However, it would be simpler and more readable to use your language's float parsing/casting functions.
EDIT: Some variations based on the comments:
Allowing 100.0 and 100.00: ^(100(\.0{1,2})?|\d{1,2}(\.\d{1,2})?|e)$
Disallowing leading zeroes: ^(100(\.0{1,2})?|[1-9]?\d(\.\d{1,2})?|e)$
^(?:100|\d{1,2}(?:\.\d{1,2})?|e)$
Hmm does this work for you?
^((100|[0-9]{1,2})(\.[0-9]{1,2})?)|(e)$
Whay environment is this for? Any particular regex standard it must adhere to?
Constraints on numeric values (such as "> 100", or "<= 5.3") can make regexes rather complicated. These types of contraints are better checkedin application logic. Then you can have a simpler (and easier to understand) pattern:
^(([0-9]{1,3})(\.[0-9]{1,2})?)|(e)$
And then extract the capture group for the first 3 digits and validate that separately.
Edit:
Ok I think this one should do it (last one because my eyes are getting tired):
^(100(\.0{1,2})?)|([0-9]{1,2})(\.[0-9]{1,2})?|(e)$
Will also allow 100.00 or 100.0