i have had a go at matching a regular expression that is given in hh:mm:ss:nnn
with the range being :
00:00:00:000 for lowest
23:59:59:999 as the highest
a valid example is 06:07:22:575
invalid example being 6:7:22:475
so far i have given it a go and hasnt worked with this :
^(([0-1][1-9])|(2[1-3])):((0[1-9])|(1-5[0-9]:((0[1-9])|(1-5[0-9]:([1-9][1-9][1-9])$
but it hasnt worked.. can someone help me out
Try this regex:
^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]:[0-9][0-9][0-9]$
The only possibly tricky part here which might require explanation is the portion of the regex which covers the first two digits. In this case, we only want to support numbers from 0 to 23 inclusive. So, we use this pattern:
(?:[01][0-9]|2[0-3])
This says to match (and not capture) either a 0 or 1 followed by 0 to 9, i.e. 00-19, or a 2 followed by 0 to 3, i.e. 20-23.
Here is a working demo:
Demo
I think you just missed some square and round brackets?
^(([0-1][1-9])|(2[1-3])):((0[1-9])|([1-5][0-9])):((0[1-9])|([1-5][0-9])):([1-9][1-9][1-9])$
When you are trying to use check number between 00 and 59, it should be
( (0[1-9])|([1-5][0-9]) )
The one you tried (0[1-9])|(1-5[0-9] will only fetch 01~09
I would recommend:
/^[0-1][0-9]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]|^2[1-3]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]$/
Comprising two parts:
^[0-1][0-9]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]
^2[1-3]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]
This will match any time under 24 hours in the format listed above.
This can be seen working on Regex101 here.
const regex = /^[0-1][0-9]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]|^2[1-3]:[0-6][0-9]:[0-6][0-9]:[0-9][0-9][0-9]$/gm;
const strings = [
"00:00:00:000",
"23:59:59:999",
"06:07:22:575",
"6:7:22:475"
]
for (let i = 0; i < strings.length; i++) {
console.log(regex.test(strings[i]));
}
Related
I came across the regular expression not containing 101 as follows:
0∗1∗0∗+(1+00+000)∗+(0+1+0+)∗
I was unable to understand how the author come up with this regex. So I just thought of string which did not contain 101:
01000100
I seems that above string will not be matched by above regex. But I was unsure. So tried translating to equivalent pcre regex on regex101.com, but failed there too (as it can be seen my regex does not even matches string containing single 1.
Whats wrong with my translation? Is above regex indeed correct? If not what will be the correct regex?
Here is a bit shorter expression ^0*(1|00+)*0*$
https://www.regex101.com/r/gG3wP5/1
Explanation:
(1|00+)* we can mix zeroes and ones as long as zeroes occur in groups
^0*...0*$ we can have as many zeroes as we want in prefix/suffix
Direct translation of the original regexp would be like
^(0*1*0*|(1|00|000)*|(0+1+0+)*)$
Update
This seems like artificially complicated version of the above regexp:
(1|00|000)* is the same as (1|00+)*
it is almost the solution, but it does not match strings 0, 01.., and ..10
0*1*0* doesn't match strings with 101 inside, but matches 0 and some of 01.., and ..10
we still need to match those of 01.., and ..10 which have 0 & 1 mixed inside, e.g. 01001.. or ..10010
(0+1+0+)* matches some of the remaining cases but there are still some valid strings unmatched
e.g. 10010 is the shortest string that is not matched by all of the cases.
So, this solution is overly complicated and not complete.
read the explanation in the right side tab in regex101 it tells you what your regex does( I think you misunderstood what list operator does) , inside a list operator ( [ ) , the other characters such as ( won't be metacharacters anymore so the expression [(0*1*0*)[1(00)(000)] will be equivalent to [01()*[] which means it matches 0 or 1 or ( or ) or [
The correct translation of the regular expression 0∗1∗0∗+(1+00+000)∗+(0+1+0+)∗
will be as follows:
^((?:0*1*0*)|(?:1|00|000)*|(?:0+1+0+)*)$
regex101
Debuggex Demo
What your regex [(0*1*0*)[1(00)(000)]*(0+1+0+)*] does:
[(0*1*0*)[1(00)(000)]* -> matches any of characters 0,(,),*,[ zero or more times followed by
(0+1+0+)* --> matches the pattern 0+1+0+ 0 or more times followed by
] --> matches the character ]
so you expression is equivalent to
[([)01](0+1+0+)*] which is not a regular expression to match strings that do not contain 101
0* 1* ( (00+000)* 1*)* (ε+0)
i think this expression covers all cases because --
any number apart from 1 can be broken into constituent 2's and 3's i.e. any number n=2*i+3*j. So there can be any number of 0's between 2 consecutive 1's apart from one 0.Hence, 101 cannot be obtained.
ε+0 for expressions ending in one 0.
The RE for language not containing 101 as sub-string can also be written as (0*1*00)*.0*.1*.0*
This may me a smaller one then what you are using. Try to make use of this.
Regular Expression I got (0+10)1. (looks simple :P)
I just considered all cases to make this.
you consider two 1's we have to end up with continuous 1's
case 1: 11111111111111...
case 2: 0000000011111111111111...(once we take two 1's we cant accept 0's so one and only chance is to continue with 1's)
if you consider only one 1 which was followed by 0 So, no issue and after one 1 we can have any number of 0's.
case 3: 00000000 10100100010000100000100000 1111111111
=>(0*+10*)1
final answer (0+10)1.
Thanks for your patience.
I've created one text field which accepts the product code.
I have tried many ways and got disappointed.
The product code is having some validations like follows,
Product code :315299AZ
1.First 2 digits ranges from[01-31].,should not contain 00.
2.Second 2 digits ranges from [01-52]., should not contain 00.
3.Third 2 digits ranges from [00-99].
4.Last 2 are optional. But should accept only alphabets. Should not accepts numbers.
Please someone help me to get out of it.
You can use the following regex :
(?!00)(([0-2][0-9])|31|30)(?!00)(([0-4][0-9])|51|50|52)(\d{2})([a-zA-Z]{2})?
(?!00) is a negative look-ahead that doesn't allows 00.
Debuggex Demo
There you go:
((0[1-9])|([1-2]\d)|(3[0-1]))((0[1-9])|([1-4]\d)|(5[0-2]))\d{2}([a-zA-Z]{2})?
If you don't like look-aheads.
I know it's not the spirit, but any sensible language supporting regular expressions should allow you to access groups, hence do something along these lines (pseudocode follows):
if product_code matches /^(\d\d)(\d\d)\d\d([a-zA-Z]{2})?$/ {
assert 1 <= int($1) <= 31 // validate first group
assert 1 <= int($2) <= 52 // validate second group
}
Bonus: you can actually read it.
(This is assuming the last optional group contains either two or zero characters. If one character is acceptable, you can replace it with [a-zA-Z]{0,2})
I have been reading the regex questions on this site but my issue seems to be a bit different. I need to match a 2 digit number, such as 23 through 75. I am doing this on an HP-UX Unix system. I found examples of 3 - 44 but or any digit number, nothing that is fixed in length, which is a bit surprising, but perhaps I am not understand the variable length example answer.
Since you're not indicating whether this is in addition to any other characters (or in the middle of a larger string), I've included the logic here to indicate what you would need to match the number portion of a string. This should get you there. We're creating a range for the second numbers we're looking for only allowing those characters. Then we're comparing it to the other ranges as an or:
(2[3456789]|[3456][0-9]|7[012345])
As oded noted you can do this as well since sub ranges are also accepted (depends on the implementation of REGEX in the application you're using):
(2[3-9]|[3-6][0-9]|7[0-5])
Based on the title you would change the last 5 to a 9 to go from 75-79:
(2[3-9]|[3-6][0-9]|7[0-9])
If you are trying to match these numbers specifically as a string (from start to end) then you would use the modifiers ^ and $ to indicate the beginning and end of the string.
There is an excellent technical reference of Regex ranges here:
http://www.regular-expressions.info/numericranges.html
If you're using something like grep and trying to match lines that contain the number with other content then you might do something like this for ranges thru 79:
grep "[^0-9]?(2[3-9]|[3-6][0-9]|7[0-9])[^0-9]?" folder
This tool is exactly what you need: Regex_For_Range
From 29 to 79: \b(2[3-9]|[3-7][0-9])\b
From 29 to 75: \b(29|[3-6][0-9]|7[0-5])\b
And just for fun, from 192 to 1742: \b(19[2-9]|[2-9][0-9]{2}|1[0-6][0-9]{2}|17[0-3][0-9]|174[0-2])\b :)
If I want 2 digit range 0-63
/^[0-9]|[0-5][0-9]|6[0-3]$/
[0-9] will allow single digit from 0 to 9
[0-5][0-9] will allow from 00 to 59
6[0-3] will allow from 60 till 63
This way you can take Regular Expression for any Two Digit Range
You have two classes of numbers you want to match:
the digit 2, followed by one of the digits between 3 and 9
one of the digits between 3 and 7, followed by any digit
Edit: Well, that's the title's range (23-79). Within your question (23-75), you have three:
the digit 2, followed by one of the digits between 3 and 9
one of the digits between 3 and 6, followed by any digit
the digit 7, followed by one of the digits between 0 and 5
Just to add to this, here is a solution for generating the string from the accepted answer in javascript. You can click "Run Code Snippet" to enter your own bounds and get your own string.
function regexRangeString(lower,upper){
let current=lower;
let nextRange=function(){
let currentString=String(current);
let len=currentString.length;
let string="";
let newUpper;
for(let digit=0;digit<len;digit++){
let index=len-digit-1;
let lower=Number(currentString[index]);
let thisString="";
for(let u=9;u>=lower;u--){
let us=currentString.substring(0,index)+u+currentString.substring(index+1,len);
if(Number(us)<=upper){
if(lower==u){
thisString=lower;
}
else{
thisString=`[${lower}-${u}]`;
}
currentString=currentString.substring(0,index)+u+currentString.substring(index+1,len);
break;
}
}
if(thisString!="[0-9]"){
string=currentString.substring(0,index)+thisString+string;
break;
}
else{
string=thisString+string
}
}
current=Number(currentString)+1;
return string
}
let string=""
while(current<upper){
string+="|"+nextRange(current);
}
string="("+string.slice(1)+")";
return string
}
let lower=prompt("Enter Lower Bound")
let upper=prompt("Enter Upper Bound")
alert(regexRangeString(lower,upper))
For example:
regexRangeString(72,189)
generates the following output string:
(7[2-9]|[8-9][0-9]|1[0-8][0-9])
This should do it:
/^([2][3-9]|[3-6][0-9]|[7][0-5])$/
^ and $ will make it strict that it will match only 2 numbers, so in case that you have i.e 234 it won't work.
I want a regex that checks the following things:
The string starts with an +
After the '+' only numbers can occur
There should be atleast 4 numbers after the +
Does anyone know how to make this?
/^+\d{4,}$/
will meet your requirements.
^ is the anchor for start fo the string
\d is a digit
{4,} says at least 4 of the preceding expression (here the \d). you can add a maximum if needed like {4,20} would allow at least 4 and at most 20 characters.
$ is the anchor for the end of the string
/^((00|\+)[0-9]{2,3}){0,1}[0-9]{4,14}$/
More general than your request, but you can specialize it. Explaining:
((00|\+)[0-9]{2,3})
international code with 00 or + and 2 or 3 digits. Modify the expression according to your needs.
{0,1}
international code is optional - remove it if it is required
[0-9]{4,14}
digits: minimum 4, maximum 14. Change the values according to your needs.
Regards
A.
/\+\d{4,15}/
This should help if 15 is the atmost limit of numbers
OR rather keep the second parameter blank as stema suggested.
I went with this one:
/\A(([+]\d{3,})?\d{6,8})/
I have a need to search all numbers with 4 digits between 2000 and 3000.
It can be that letters are before and after.
I thought I can use [2000-3000]{4}, but doesnt work, why?
thank you.
How about
^2\d{3}|3000$
Or as Amarghosh & Bart K. & jleedev pointed out, to match multiple instances
\b(?:2[0-9]{3}|3000)\b
If you need to match a3000 or 3000a but not 13000, you would need lookahead and lookbefore like
(?<![0-9])(?:2[0-9]{3}|3000)(?![0-9])
Regular expressions are rarely suitable for checking ranges since for ranges like 27 through 9076 inclusive, they become incredibly ugly. It can be done but you're really better off just doing a regex to check for numerics, something like:
^[0-9]+$
which should work on just about every regex engine, and then check the range manually.
In toto:
def isBetween2kAnd3k(s):
if not s.match ("^[0-9]+$"):
return false
i = s.toInt()
if i < 2000 or i > 3000:
return false
return true
What your particular regex [2000-3000]{4} is checking for is exactly four occurrences of any of the following character: 2,0,0,0-3,0,0,0 - in other words, exactly four digits drawn from 0-3.
With letters before an after, you will need to modify the regex and check the correct substring, something like:
def isBetween2kAnd3kWithLetters(s):
if not s.match ("^[A-Za-z]*[0-9]{4}[A-Za-z]*$"):
return false
idx = s.locate ("[0-9]")
i = s.substring(idx,4).toInt()
if i < 2000 or i > 3000:
return false
return true
As an aside, a regex for checking the range 27 through 9076 inclusive would be something like this hideous monstrosity:
^2[7-9]|[3-9][9-9]|[1-9][0-9]{2}|[1-8][0-9]{3}|90[0-6][0-9]|907[0-6]$
I think that's substantially less readable than using ^[1-9][0-9]+$ then checking if it's between 27 and 9076 with an if statement?
Hum tricky one. The dash - only applies to the character immediately before and after so what your regex is actually matching is exactly 4 characters between 0 and 3 inclusive (ie, 0, 1, 2 and 3). eg, 3210, 1230, 3333, etc... Try the expression below.
(2[0-9]{3})|(3000)
Here's explanation why and ways to detect ranges: http://www.regular-expressions.info/numericranges.html
Correct regex will be \b(2\d{3}|3000)\b. That means: match character '2' then exactly three digits (this will match any from 2000 to 2999) or just match '3000'. There are some good tutorials on regular expressions:
http://gnosis.cx/publish/programming/regular_expressions.html
http://immike.net/blog/2007/04/06/the-absolute-bare-minimum-every-programmer-should-know-about-regular-expressions/
http://www.regular-expressions.info/
why don't you check for greater or less than? its simpler than a regex
num >= 2000 and num <=3000