I have the following RegEx:
(^\d{1,3}$)|(\d{1,3})\.?(\d{0,0}[0,5])
This accepts any .5 increment of a number.
I want to add a range to this number of 13.5. Ideally, .5 should be valid to.
So, anything from .5 to 13.5, in .5 increments.
Examples allowed:
.5
4
12.5
13.5
Not allowed:
.56
3.45
14
14.5
Your current regex has some big issues e.g. [0,5] doesn't mean either 0 or 5 but a 0, , or 5. Try the following regex instead:
^(?=.)(?:\d|1[0-3])?(?:\.5)?$
See live demo here
Based on your given allowed examples it matches 12 but doesn't match 12.0. If you want to match 12.0 as well you have to replace \.5 with \.[05]:
^(?=.)(?:\d|1[0-3])?(?:\.[05])?$
Note: postive lookahead (?=.) ensures that an empty match doesn't occur.
^([0-9]|1[0-3])?(\.[05])?$
This works for your given examples
Related
Hi I'm looking for a regular expression that would match only numbers that represent percentages with up to 4 decimals. meaning for example:
should match
should not match
100
100.0001
99.9999
101
0.0001
100.00001
0
Iv'e tried this but it does not work (does not match 0.0004 for example):
"^\\d{1,3}\\.\\d{1,4}$"
Thanks!
I think the best way to aproach this is by considering 100% to be a special case.
^(100(\.0{1,4})?|\d{1,2}(\.\d{1,4})?)$
That way you can match 0->99.9999 or 100 with up to 4 decimal 0s after it.
demo
Use alterations to capture the different possibilities.
This works:
^((?:100)|(?:100\.0{1,4})|(?:\d{1,2})|(?:\d{1,2}\.\d{0,4}))$
Demo
I have a requirement to restrict the numbers between 3.00 to 100.00
I used below expression
^([3-9]|[1-9][0-9]|100)+(\.\d{1,2})?$
The issue with above expression is that, it's allowing 100.01 to 100.99,which should be restricted.It also allows 310 to 399,which needs to restricted.
I used another flavor of same expression
^([3-9]|[1-9][0-9]|100.00)+(\.\d{1,2})?$
Which was working as expected,but we need to enter 100.00 in to pass the regular expression instead of 100.
Is there any way,I can achieve the desired result?
When alternating with the final 100, use negative lookahead for \.\d?[1-9], to ensure that the decimal places, if any, have only 0s.
Your first pattern can also match many repeated digits before the optional decimal (like 333 and 101010) due to the + at the end of the group, so best to remove the + if you only want to match between 3 and 100.
^(?:[3-9]|[1-9][0-9]|100(?!\.\d?[1-9]))(?:\.\d{1,2})?$
^^^^^^^^^^^^^^
https://regex101.com/r/tJd3LQ/1
To permit leading zeros, add 0* right after the ^:
^0*(?:[3-9]|[1-9][0-9]|100(?!\.\d?[1-9]))(?:\.\d{1,2})?$
^^
You can try this mate
^(?:100(?:\.0+)?|(?:[3-9]|[1-9][0-9])(?:\.\d{1,2})?)$
Demo
Explanation
^ - Anchor represent start of string.
(?:100(?:\.0+)?) - This will match 100, 100.0, 100.00(any number of decimal zero's).
| - Alternation this works same as Logical OR.
(?:[3-9]|[1-9][0-9])(?:\.\d{1,2})?) - This will match any number from 3.00 to 99.99
Suggestion
Always use non capturing group in case you're not using the group any where else again in your regex.
The problem with this: ^([3-9]|[1-9][0-9]|100)+(\.\d{1,2})?$, is that it also allows 100 in the first chunk (the chunk responsible for allowing whole numbers).
In your case, you would need to use something like so: ^100\.00$|([3-9]|[1-9][0-9])+(\.\d{1,2})?$ (Example here).
This expression will either try to match 100.00 as a whole (which is your upper bound, or else, any number between 3.00 up till 99.99.
Maybe this regex will work for You:
^([3-9]|[1-9][0-9])(\.[0-9]+)?$|^100(\.0+)?$
Test:
$ cat numeric.txt
0.0
3
3.0
3.00
3.001
2.99
99.99
99.999
100
100.0
100.00
100.01
100.99
$ egrep '^([3-9]|[1-9][0-9])(\.[0-9]+)?$|^100(\.0+)?$' numeric.txt
3
3.0
3.00
3.001
99.99
99.999
100
100.0
100.00
I need to only accept input that meets these rules...
0.25-24
Increments of .25 (.00, .25, .50, .75)
First digit doesn't have to be required.
Would like trailing zeros to be optional.
Examples of some valid entries:
0.25
.50
.5
1
1.0
5.50
23.75
24 (max allowed)
UPDATE: nothing at all, null/blank, should also be accepted as valid
Example of some invalid entries:
0
.0
.00
0.0
0.00
24.25
-1
I understand that RegEx is a pattern matching language therefore it's not great for ranges, less-than, and great-than checking. So to check if it's less than or equal to 24 means I'd have to find a pattern, right? So there are 24 possible patters which would make this a long RegEx, am I understanding this correctly? I could use ColdFusion to do the check to make sure it's in the 0-24 range. It's not the end of the world if I have use ColdFusion for this part, but it'd be nice to get it all into the RegEx if it doesn't cause it to be too long. This is what I have so far:
^\d{0,2}((\.(0|00|25|5|50|75))?)$
http://regex101.com/r/iS7zM3
This handles pretty much all of it except for the 0-24 range check or the check for just a zero. I'll keep plugging away at it but any help would be appreciated. Thanks!
Change \d{0,2} to (?:1[0-9]?|2[0-4]?|[3-9])? and it'll match from 1 to 24 (or nothing).
You can also simplify the second part to (?:\.(?:00?|25|50?|75))? - you could go further to (?:\.(?:[05]0?|[27]5))? but that might obfuscate the intent a bit too far.
To exclude 24.25 you could perhaps use a negative lookahead (?!24\.[^0]) to prevent anything other than 24.0 or 24.00, but it's probably simpler to just exclude 24 from the main pattern and include a specific check for 24/24.0/24.00 at the start:
(?x)
# checks for 24
^24$|^24\.00?$
|
# integer part
^
(?:1[0-9]?|2[0-3]?|[3-9]|0(?=\.[^0])|(?=\.[^0]))
# decimal part
(?:\.(?:00?|25|50?|75))?
$
That also includes a check for 0(?=\.[^0]) which uses a positive lookahead to only allow an initial 0 if the next char is a . followed by a non-zero (so 0.0 and 0.00 isn't allowed).
The (?x) flag allows whitespace to be ignored, allowing readable regex in your code - obviously preferable to squashing it all onto a single line - and also enables the use of # to start line comments to explain parts of a pattern. (Literal whitespaces and hashes can be escaped with backslash, or encoded via e.g. \x23 for hash.)
For comparison, here's a pure-CFML way of doing it:
IsNumeric(Num)
AND Num GT 0
AND Num LTE 24
AND NOT find('.',Num*4)
Now, are you really sure it's better as a regex...
You could try this regex (broken down):
^
(?:
(?:[1-9]|1\d|2[0-3])(?:\.(?:[05]0?|[27]5))? # Non-zeros with optional decimal
|
0?(?:\.(?:50?|[27]5)) # Decimals under 1
|
24(?:\.00?)? # The maximum
)
$
In one line:
^(?:(?:[1-9]|1\d|2[0-3])(?:\.(?:[05]0?|[27]5))?|0?(?:\.(?:50?|[27]5))|24(?:\.00?)?)$
regex101 demo
^([0-1]?[0-9]|2[0-4])((\.(0|00|25|5|50|75))?)$
This means the one's place can be 0-9 if the tens place is missing, a 0, or 1.
If the tens place is a 2, then the ones place can be 0-4.
The second part is great, it's simple and readable too. It has an extra set of parens though that can be removed, reducing it to this:
^([0-1]?[0-9]|2[0-4])(\.(0|00|25|5|50|75))?$
I should create a regular expression that match all following values.. these values are percentages, so max value is 100:
1
1.
1.11
I do not manage to match "1.".
I have tried different regular expression... but with no success... I think the one nearest to the solution is
(\d{1,2})|((\d{1,2})(\.)?((\d{1,5})?))
But it still does not work...
Does anyone can help me please?
How about:
\b(?:100|\d\d?(?:\.\d{0,5})?)\b
This regex matches your test cases, plus a few more edge cases:
(?<=^|\s)(?=[\d.])(100|\d{0,2})(\.\d{0,5})?(?=\s|$)
See a live demo matching all of these:
.1
.12345
0.1
1
1.
1.11
12
99
100
But not:
(blank)
12.123456
111
1111
What is the RegEx for value Range from 1- 365
Try this:
^(?:[1-9]\d?|[12]\d{2}|3[0-5]\d|36[0-5])$
The start anchor ^ and end anchor
$ are to match the whole input and
not just part of it.
(? ) is for grouping.
| is for alternation
[1-9]\d? matches 1 to 99
[12]\d{2} matches 100 to 299
3[0-5]\d matches 300 to 359
36[0-5] matches 360 to 365
You would have to list the possible combinations 1-9, 10-99, 100-299, 300-359, 360-365:
^([1-9]\d?|[12]\d\d|3[0-5]\d|36[0-5])$
Not really a good fit for regex, but if you insist:
^(?:36[0-5]|3[0-5][0-9]|[12][0-9][0-9]|[1-9][0-9]|[1-9])$
This is not allowing leading zeroes. If you wish to allow those, let me know.
The expression above can be shortened a little to
^(?:36[0-5]|3[0-5]\d|[12]\d{2}|[1-9]\d?)$
but I find the first solution to be a bit more readable. YMMV.
A general solution for matching the numbers from 1 to XYZ
^(?!0)(?!\d{4}$)(?![X+1-9]\d{2}$)(?!X[Y+1-9]\d$)(?!XY[Z+1-9]$)\d+$
Notes:
If any of X, Y or Z are 9 that will make X+1 etc. be 10. If that happens the regex part that would require using the 10 should be left out.
This can be extended to numbers with more or less digits following the same principles.
It does not allow left-padding 0es.
Applied to your case:
^(?!0)(?!\d{4}$)(?![4-9]\d{2}$)(?!3[7-9]\d$)(?!36[6-9]$)\d+$
Lets explain:
(?!0\d*) - does not start with 0
(?!\d{4}$) - does not have 4 digits, i.e. between 1000 and infinity
(?![4-9]\d{2}$) - it's not between 400 and 999
(?!3[7-9]\d$) - it's not between 370 and 399
(?!36[6-9]$) - it's not between 366 and 369
Test it.
^36[0-5]|(3[0-5]|[12]?[0-9])[0-9]$
^3(6[0-5]|[0-5]\d)|[12]\d\d|[1-9]\d|[1-9]$
Or if numbers like 05 can not be in input:
^3(6[0-5]|[0-5]\d)|[12]?\d?\d$
P.S.: Anyway no need of regex here. Use ToInt(), <=, >=
It really depends on your regex engine since they may not all be PCRE-style. I usually work to the lowest common denominator unless I know it will be targeting a minimum engine.
To that end, I'd just use something like:
^[1-9]|[1-9][0-9]|[1-2][0-9]{2}|3[0-5][0-9]|36[0-5]$
This will take care of (in order):
1-9.
10-99.
100-299.
300-359.
360-365.
However, unless you're absolutely required to use just a regex, I wouldn't. It's like trying to kill a fly with a thermo-nuclear warhead.
Just use the much simpler ^[0-9]{1,3}$ then use whatever language features you have to convert it to an integer and check it's between 1 and 365 inclusive:
def isValidDayOtherThanLeapYear (s):
if not s.matches ("^[0-9]{1,3}$"):
return false
n = s.toInteger()
if n < 1 or n > 365:
return false
return true
Your code will be more readable that way and I tend to rethink the use of regular expressions the second they start looking like they may be hard to read six months down the track.
This worked for me...
^[1-3][0-6]?[0-5]?$