regex 4-6 digits ending with 00, but lowest value is 5000 - regex

i'm trying to make a regex for this condition:
6 numbers max
finish always with 00
lowest input 5000
These should match:
5000
11100
699900
999900
These should not match:
900
4900
12345
999999
1230000
I browsed through the site, but without success.
I'm not very good at regex - could somebody please help?

Use the following
^([5-9]\d|[1-9]\d{2,3})00$

I think you mean 5000 or above:
^([5-9]\d|[1-9]\d{2,3})00$
See a live demo.

I think that this should match all of them. We deal with the 4 digit case separately:
^([1-9][0-9]{2,3}00)|([5-9][0-9]00)$

^[5-9][0-9]{1,3}00
So has to start with a digit between 5 and 9 ^[5-9]
Then can have any number between 0-9 1, 2 or 3 times and must end with 00
Look at the following demo

^([5-9]\d{1,3}|[1-4]\d{2,3})00$

Related

Limiting parts and total of numbers in a string (regex)

I'm trying to use regex to find tax numbers with the formats:
nnn-nnn-nnn | nn-nnn-nnn
nnn nnn nnn | nn nnn nnn
nnnnnnnnn | nnnnnnnn
EDIT: some samples are 062-225-505, 62-225-505, 062 225 505, 62 225 505, 062225502, 62225505. The numbers should not be any longer than 9 numbers in total
So far I have ([0-9]{2,3}(\s|-|)+[0-9]{3,8}(\s|-|)+[0-9]{3,9})
This works, BUT it is also finding 050821862257111 which is too long for what I'm trying to find. How do I limit the total string as well as each part being limited?
Thanks!
Try ^\d{1,9}(?:(?:-| )\d{1,9})*$
Explanation:
^ - match beginning of a string
\d{1,9} - match between 1 and 9 digits
(?:...) - non-captuirng group
-| - alterantion: match or -
* - match zero or more times
$ - match end of a string
Demo
With a small change to your regex, you can limit the length to eight or nine numbers, although this would still allow a mix and match of the delimiters:
([0-9]{2,3}[\s-]?[0-9]{3}[\s-]?[0-9]{3})
If the actual number of delimiters is not important, then you could just remove then, and then just check the length of the remaining numbers.
^\d{2}\d?(?:-|\s)?\d{3}(?:-| )?\d{3}$
demo at regex101
This regex will only match if the spaces and dashes are in the right place.
This will match: 062-225-505
This will not match: 062-2255-05 or 062225--505
Found with a combination of all of your help! :)
\s\d{2,3}\d?(-|\s)?\d{3}(\1)?\d{3}(?!\d)
Found 62-225-505, 62225505, 062 225 505, and did not find 060821067254101
Thanks all :)

how to make a regular expressions to accept only digits not started with zero or zero only?

I'm trying to create a regex to accept digits not starting with zero or a single zero digit.
Example matches
0
50
798
Example rejects
01
046
0014
00
0001
My attempt was to use /[0]|[1-9][0-9]*/ to match the values in the following text:
0, 50, 798
01, 046, 0014, 00, 0001
This attempt can be run at http://regexr.com/3bb00
Use following regex :
^(0|[1-9]\d*)$
see Demo https://regex101.com/r/zT8uI2/2
This regex contains 2 part, 0 or [1-9]\d* which is a digit that doesn't starts with zero.
Note that if you want to match your numbers within other texts you need a word boundary instead of start and end anchors :
\b(0|[1-9]\d*)\b
see demo https://regex101.com/r/zT8uI2/3
It seems that you have two cases in your regex:
Match a single zero
Match digits that don't start with zero.
The first case is easy: /0/
The second case is also pretty easy /[1-9]\d*/. The [1-9] matches the digit that is not 0. Then, we can have 0 or more digits.
To get both of these cases, just use a bar to do either or
/0|[1-9]\d*/
Hmm, why not something like..
if(input[0] == '0' && input.size() > 1) // reject
else //accept
Please check this http://regexr.com/3bb09
Took the tip from https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch06s06.html
and improved it to negate numbers starting with 0.
RE: \b[^0,]*([1-9][0-9]*|0)\b
Text: 0, 50, 798, 01, 046, 0014, 00, 0001
Matched only 0, 50 and 798
Thanks
Venkat

What's the best Regular Expression to use for returning some phone numbers, but not all?

I'm new to Regular expressions and working on something that will return all UK phone numbers with an area code beginning 01, 02, 03 or 07 only. It has to not look up 08 or 09. It also has to take in to account the different grouping styles too. But here's the kicker... it's got to be 80 characters or less.
This was my best shot:
(01|02|03|07|44\D*1|44\D*2|44\D*3|44\D*7|)(\d\D*){9}
The problem is that it's returning any 9 digit or less number and I can't figure out why.
Any help would be grand!
(01|02|03|07|44\D*1|44\D*2|44\D*3|44\D*7) is matching either 0 or 44\D* followed by 1, 2, 3 or 7 which simplifies to:
(?:44\D*|0)[1237]
Putting that with the rest gives:
(?:44\D*|0)[1237](\D*\d\D*){9}
Debuggex Demo

Regular Expression - Two Digit Range (23-79)?

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.

Violating RegExp: Every string that is smaller or equal "001700"

i have a unique challenge.
i want to create a google analytics filter for a custom variable that only returns a value if the given string is smaller or equal than '001700'. yeah, i know that a string can't be smaller, still i need to find a way to make this work.
oh, and if you ask: no there is no way to convert that string to a number (according to my knowledge - via a google analytics filter - and that is what i have to work with in this case).
so basically, i have
000000
000001
000002
000003
...
...
999998
999999
and i need a regular expression that matches
001700
001699
001698
...
...
000001
000000
but does not match
001701
001702
...
...
999998
999999
sub question a) is it possible? (as i have learned, everything is possible with regExp if you are clever and/or masochistic enough)
sub question b) how to do it?
thx very much
You can do:
^00(1700|1[0-6][0-9]{2}|0[0-9]{3})$
See it
yes you can do
see this article
Eg:
alert('your numericle string'.replace(/\d+/g, function(match) {
return parseInt(match,10) <= 17000 ? '*' : match;
}));
JavaScript calls our function, passing
the match into our match argument.
Then, we return either the asterisk
(if the number matched is under 17000) or
the match itself (i.e. no match should
take place).
Can be done with RegEx:
/00(1([0-6][0-9]{2}|700)|0[0-9]{3})/
Explanation:
00 followed by
1 followed by 0 to 6 and any 2 numbers = 1000 - 1699
or
1700
or
0 followed by any 3 numbers = 0000 - 0999