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
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'm trying to find a regular expression for a float with a fixed maximum (for example 4) number of significant figures.
this should match with:
- 123.4
- 12.34
- 1.2
- 223
- 0.1234
- 0.000001234
the problem is that the number of non-zeros before and after the dot has to be at most 4 in total.
I tried to split the problem and found solutions for the cases:
- 0.xxxx
- 0.000xxx
- xxxx
But I didn't find a solution for the case that significant digits are found before and after the dot. (examples: 1.23 2.345 )
update:
I think I found a solution:
^(?!(?:.*[1-9](\.?[0-9]){4,}))([-+]?\d+\.?\d*?)$
^(?!(?:.*?[1-9]){5,})([-+]?\s*\d+\.?\d*?)$
Try this.This will match only 4 or less significant digits.Do not forget to put flags g and m.See demo.
http://regex101.com/r/hQ1rP0/28
I think you want something like this,
^0*(?:[1-9]\d{0,3})?(?:\.0*(?:[1-9]\d{0,3})?)?$
DEMO
I have been trying with a regex ^\d{0,12}(\.{0,1}\d{0,2})$.
Valid matches:
.22
0.22
123456789012.01
123.2
123.
125
120.00
125444
123456789123.
Invalid match
1234567891232
12345678912345
How to restrict invalid matches as this regex is working fine for all other cases
now i have to restrict the digits before decimal to just 12, i hope now u all will get what i need.
I think you want something like this,
^(?:\d{0,12}\.\d{0,2}|\d{0,12})$
DEMO
I searched a lot and can't find the solution for this RegExp (I have to say I'm not very experienced in Reg. Expressions).
Regex = ^[1-9]?[0-9]{1}$|^100$
I would like to test a number between 1 and 100, excluding 0
Try:
^[1-9][0-9]?$|^100$
Working fiddle
EDIT: IF you want to match 00001, 00000099 try
^0*(?:[1-9][0-9]?|100)$
FIDDLE
For integers from 1 to 100 with no preceding 0 try:
^[1-9]$|^[1-9][0-9]$|^(100)$
For integers from 0 to 100 with no preceding 0 try:
^[0-9]$|^[1-9][0-9]$|^(100)$
Regards
Try it,
This will work more efficiently..
1. For number ranging 00 - 99.99 (decimal inclusive)
^([0-9]{1,2}){1}(\.[0-9]{1,2})?$
Working fiddle link
https://regex101.com/r/d1Kdw5/1/
2.For number ranging 1-100(inclusive) with no preceding 0.
(?:\b|-)([1-9]{1,2}[0]?|100)\b
Working Fiddle link
http://regex101.com/r/mN1iT5/6
Here are very simple regex's to understand (verified, and no preceding 0)
Between 0 to 100 (Try it here):
^(0|[1-9][0-9]?|100)$
Between 1 to 100 (Try it here):
^([1-9][0-9]?|100)$
If one assumes he really needs regexp - which is perfectly reasonable in many contexts - the problem is that the specific regexp variety needs to be specified. For example:
egrep '^(100|[1-9]|[1-9][0-9])$'
grep -E '^(100|[1-9]|[1-9][0-9])$'
work fine if the (...|...) alternative syntax is available. In other contexts, they'd be backslashed like \(...\|...\)
There are many options how to write a regex pattern for that
^(?:(?!0)\d{1,2}|100)$
^(?:[1-9]\d?|100)$
^(?!0)(?=100$|..$|.$)\d+$
I use for this angular 6
try this.
^([0-9]\.[0-9]{1}|[0-9]\.[0-9]{2}|\.[0-9]{2}|[1-9][0-9]\.[0-9]{1}|[1-9][0-9]\.[0-9]{2}|[0-9][0-9]|[1-9][0-9]\.[0-9]{2})$|^([0-9]|[0-9][0-9]|[0-99])$|^100$
it's validate 0.00 - 100. with two decimal places.
hope this will help
<input matInput [(ngModel)]="commission" type="number" max="100" min="0" name="rateInput" pattern="^(\.[0-9]{2}|[0-9]\.[0-9]{2}|[0-9][0-9]|[1-9][0-9]\.[0-9]{2})$|^([0-9]|[0-9][0-9]|[0-99])$|^100$" required #rateInput2="ngModel"><span>%</span><br>
Number should be between 0 and 100
Regular Expression for 0 to 100 with the decimal point.
^100(\.[0]{1,2})?|([0-9]|[1-9][0-9])(\.[0-9]{1,2})?$
Just for the sake of delivering the shortest solution, here is mine:
^([1-9]\d?|100)$
working fiddle
1 - 1000 with leading 0's
/^0*(?:[1-9][0-9][0-9]?|[1-9]|1000)$/;
it should not accept 0, 00, 000, 0000.
it should accept 1, 01, 001, 0001
between 0 and 100
/^(\d{1,2}|100)$/
or between 1 and 100
/^([1-9]{1,2}|100)$/
This is very simple logic, So no need of regx.
Instead go for using ternary operator
var num = 89;
var isValid = (num <= 100 && num > 0 ) ? true : false;
It will do the magic for you!!
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]?$