I need regex Expression for Floating and whole numbers that have the limit like it will accept 1 or 2 digit before point and 1 or 2 digits after point. Whole number limit should be 2 digit.
What should be valid:
- 1.1
- 11.1
- 1.11
- 11.11
- 2
- 22
What should be invalid:
- 111.111
- 222
Here is my Regex:
/^\d{1,2}(\.\d){1,2}?$/
But it is not working properly
kindly help me in this
Use the following pattern:
^\d{1,2}(?:\.\d{1,2})?$
See the regex demo.
Details:
^ - start of string
\d{1,2} - 1 or 2 digits
(?:\.\d{1,2})? - an optional sequence of:
\. - a dot
\d{1,2} - 1 or 2 digits
$ - end of string.
Try this:
/^\d{1,2}(\.\d{1,2})?$/
Making the 2nd part of the regex optional
Related
I am just curious if this regex expression will be able to be shorten. it should allow ten digits.
/^(?!0+(?:\.0+)?$)(\d{1,2}\.\d{1,10}|\d{1,2}|(100)|(100\\.00)|(100\\.0000000000)|(100\\.000000000)|(100\\.00000000)|(100\\.0000000)|(100\\.000000)|(100\\.00000)|(100\\.0000)|(100\\.0000)|(100\\.000)|(100\\.0))$/
You need to use
/^(?![0.]+$)(?:\d{1,2}(?:\.\d{1,10})?|100(?:\.0{1,10})?)$/
See the regex demo.
Details:
^ - start of string
(?![0.]+$) - no only 0 and/or . chars till end of string are allowed
(?: - either of
\d{1,2}(?:\.\d{1,10})? - one or two digits and then an optional occurrence of a . and one to ten digits
| - or
100(?:\.0{1,10})? - 100 and then an optional occurrence of a . and one to ten 0 chars
) - end of the group
$ - end of string.
I need Help for currency regex in Angular.
I'm not really good at regex.
What I want is a regex that:
allows comma as digital-group-separator, but not in the beginning or the end.
allows only 2 digits rounded after decimal point.
allows only one decimal point and not in the beginning or the end.
not allows 0.00 or 0.
This is my regex:
(?=.*?\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|\d+)?(\.\d{1,2})?$
but this regex allows 0.00
any one here please help thanks
Desired Outputs
Valid:
1,000.00
1000
0.01
24
1,234,000
11,222,245.22
Not Valid:
,000.00
,,,,,9
0
0.00
1,22,2,
1,000.
123,123,22
000,300.00
000300.00
000,123
000,000
00,000
0,000
You may use
/^(?![0,.]+$)(?:0|[1-9]\d{0,2}(?:,\d{3})*|[1-9]\d*)(?:\.\d{1,2})?$/
See the regex demo
Details
^ - start of string
(?![0.,]+$) - a negative lookahead that fails the match if there are one or more 0, , or . chars till end of string
(?:0|[1-9]\d{0,2}(?:,\d{3})*|[1-9]\d* - either of the three alternatives:
0 - zero
| or
[1-9]\d{0,2}(?:,\d{3})* - one to three digits with the first one being non-zero, and then 0 or more repetitions of a comma and then three digits
| - or
[1-9]\d* - 1+ digits with the first one being non-zero,
(?:\.\d{1,2})? - an optional sequence of a . and then 1 or 2 digits
$ - end of string.
Zambian National Registration Numbers(NRC) follow a pattern of 6 digits followed by a forward slash, followed by 2 digits, then another forward slash and then 1 digit at the end. An example of an NRC number would be 111111/11/1.
What regular expression can I use to match this format of numbers and slashes.
This format should work ^\d{6}\/\d{2}\/\d{1}$
^ - means match at the start of the string
\d{6} - means match exactly 6 digits
\/ - means match the forward slash.
\d{2} - means match exactly 2 digits
\d{1} - means match exactly 1 digit
$ - means match at the end of the string
You can test the regex expression on this site: https://regex101.com/
and this also, and is the more portable solution:
^[0-9][0-9][0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9]$
/^\(?([0-9]{6})\)?\/([0-9]{2})\/([0-9]{1})$/
This should work too
The format of zambian NRC is [ , 6 digits between 0 and 9 ] / [, 2 digits between 0 and 9] / [, 1 which can either be 1 for zambian or 2 for a non zambian ]
therefore any derivative of the following should work:
^[0-9][0-9][0-9][0-9][0-9][0-9]/[0-9][0-9]/[1-2]
I have
([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])
which works for single input as:
0
1
65
6553
but i want them for comma separated input string as:
0,1,65,6553 ->this is a valid string
65535,-1,25 ->this is a invalid string because of negative number.
please can anyone suggest solution
Note:
I have already tried repetition as:
^([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])+(,(([0-5]?\d?\d?\d?\d|6[0-4]\d\d\d|65[0-4]\d\d|655[0-2]\d|6553[0-5])))*$
which is accepting 65537 also which is undesirable.
Checking number bounds afterwards seems more straightforward to me, but anyway, this is a regex you may use (I refactored the integer part a little bit)
^(([0-5]?\d{0,4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(,|(?=$)))+$
https://regex101.com/r/1RpNuy/1
Details:
^ : String start
( : Group start
([0-5]?\d{0,4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]) : Match a number
(,|(?=$)) : Match either , or make sure this is end of line (but without reading the $)
)+ : End of group, repeat as many times as possible
$ : End of string
You may use
^(?:\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(?:,(?:\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))*$
In PCRE and Onigmo, you may use a shorter pattern where \g<1> repeated Group 1 pattern:
^(\d{1,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(?:,\g<1>)*$
See the regex demo and regex demo #2
The regex is basically ^<BLOCK>(?:,<BLOCK>)*$ where the BLOCK pattern is a regex matching the numbers from 0 to 65535:
\d{1,4} - 1, 2, 3 or 4 digits (0 - 9999)
[1-5]\d{4} - 1 to 5 digit and then any 4 digits (10000 - 50000)
6[0-4]\d{3} - 6, then a digit from 0 to 4, and then three digits (60000 - 64999)
65[0-4]\d{2} - 65, a digit from 0 to 4 and then any two digits (65000 - 65499)
655[0-2]\d - 655, a digit from 0 to 2 and then any digit (65500 - 655299)
6553[0-5] - 6553 and then a digit from 0 to 5 (65530 - 65535)
The general pattern:
^ - start of string
<BLOCK> - BLOCK pattern described above
(?:,<BLOCK>)* - 0 or more repetitions of , and then BLOCK pattern
$ - end of string.
So i got this regex code /[0-3][0-9][0-1][1-9]\d{2}[-\s]\d{4}?[^0-9]*|[0-3][0-9][0-1][1-9]\d{2}\d{4}/
This regex code take this kind of numbers:
1002821187
100282 1187
100282-1187
But i found out i dont want the numbers: 1002821187
So is it possible to make 1 regex code that only finds:
100282 1187
100282-1187
Your regex contains an alternation that matches the numbers with and without the space or -. You need to require that space or hyphen:
^[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}$
^^^^^
See the regex demo. If you do not need to check for any boundaries, remove ^ and $ anchors that make the pattern match the whole string and use [0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}. Or use word boundaries to find whole words, \b[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}\b.
Details
^ - start of string
[0-3] - a digit from 0 to 3
[0-9] - any digit
[0-1] (=[01]) - 0 or 1
[1-9] - any digit other than 0
[0-9]{2} - 2 digits
[-\s] - a - or whitespace
[0-9]{4} - 4 digits
$ - end of string.
Though you did not specify exactly what you are trying to do, here you can try the following regex :
\b\d{6}-\d{4}\b|\b\d{4}\b|\b\d{6}\b
Hope it helps.