I have the following type of strings with numbers within:
(12 - 17)
(4.5 - 5.5)
(4 - 10)
My code which works for the first two examples is like this:
def numbers=range=~/\d{1,3}.?\d{1,2}?/
where the result for numbers is :
[12,17]
[4.5,5.5]
but for the last is only
[10] it does not get the 4.
Does anyone see where I am wrong?
Your regex requires at least 2 integer digits on end. Look: \d{1,3} matches 1 to 3 digits, .? matches any character but a newline 1 or 0 times (optional) and \d{1,2}? matches 1 or 2 digits (the {1,2}? is a lazy version of a limiting quantifier meaning it will match as few digits as possible to return a valid match).
Use
/\d{1,3}(?:\.\d{1,2})?/
See the regex demo.
Explanation:
\d{1,3} - 1 to 3 digits
(?:\.\d{1,2})? - 1 or 0 sequences (due to ?) of:
\. - a literal period
\d{1,2} - 2 or 1 digits (this is a greedy version of the limiting quantifier).
Here is a Groovy demo:
def x = "(12 - 17)(4.5 - 5.5)(4 - 10)"
def res = x.findAll(/\d{1,3}(?:\.\d{1,2})?/)
println res
Output: [12, 17, 4.5, 5.5, 4, 10]
Related
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.
I am trying to write a regex that can match the following instructions
A sequence of character with the “AT” prefix, followed by “nG” where n is a digit from 1 through 5 and then "G" and lastly followed by a suffix of 5 numeric digits.
Note: just the ordinary regular expression not language specific.
An example of a matching string is this: “AT1G01040”
Here is what I could construct AT[1-5]G(d\{1,5}) but I am not sure if it is the correct answer.
Please, I need your hand on this thanks.
If the number of digits at the end may be from 1 to 5, you may use
^AT[1-5]G[0-9]{1,5}$
See the regex demo.
Note that if the number of digits at the end must be exactly 5, you must remove 1,:
^AT[1-5]G[0-9]{5}$
Details
^ - start of string
AT - a sequence of chars AT
[1-5] - 1, 2, 3, 4 or 5
G - a G char
[0-9]{1,5} - any 1 to 5 consecutive occurrences of an ASCII digit (or - if you use {5} - exactly 5 occurrences)
$ - end of string.
import re
reg = r'^[(][+-]?([0]|([1-9][0-9]*)\.?\d+?),\s[+-]?([0]|([1-9][0-9]*)\.?\d+?)[)]$'
for _ in range(int(input())):
coord = input()
if re.search(reg, coord):
if 0 <= float(re.search(reg, coord).group(1)) <= 90 and 0 <= float(re.search(reg, coord).group(3)) <= 180:
print('Valid')
else: print('Invalid')
else: print('Invalid')
Here is my code for a regular expression that finds coordinates. I had trouble finding the mistake in the regular expression. The test cases that do not work are (-6, -165) and (-6, -172) What is the problem that prevents the code to enter the first if statement?
The main issue is that \d+? matches 1 or more digits, as few as possible, while you assumed it matches 0 or more digits.
To make .xxx part optional, use an optional non-capturing group (?:\.\d+)?:
^\([+-]?((?:0|[1-9][0-9]*)(?:\.\d+)?),\s[+-]?((?:0|[1-9][0-9]*)(?:\.\d+)?)\)$
See the regex demo
The part that matches a number, (?:0|[1-9][0-9]*)(?:\.\d+)?, now matches:
(?:0|[1-9][0-9]*) - a non-capturing group matching either of the 2 alternatives:
0 - a zero
| - or
[1-9][0-9]* - a digit from 1 to 9 and then any 0+ digits
(?:\.\d+)? - an optional non-capturing group matching 1 or 0 occurrences of:
\. - a dot
\d+ - 1 or more digits.
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