regex number range issue, can't stop - - regex

Hi I am using this to generate ranges on a switch: http://code.google.com/p/klish/wiki/subcommands
I have set the pattern to be 0-255. This works fine
<PTYPE name="MAX_LEARN_ADDR"
method="integer"
pattern="0..255"
/>
This correctly only accepts 0-255 and stops things like * $ £ saying they are invalid. However - causes an error. I tried:
^([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$
Same thing happens.
sample legal input:
switch(config-if)# switchport port-security maximum 3
%INFO: port-security maximum is 3, up to 3 DYNAMIC addresses will be learned
Sample illegal input:
switch(config-if)# switchport port-security maximum *
Syntax error: Illegal parameter
However:
switch(config-if)# switchport port-security maximum -
gives a python trace because the - is not being caught as an invalid parameter and is getting passed to the function.

^([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$ would be what you need

Why don't you try using this instead for the pattern you are supposed to allow?
Edit 2 Alright, this should do the trick!
^(25[0-5]|[2][0-4]\d|[1]\d\d|[1-9]\d|\d)$

this regex might do the work for you.
^25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d$
The first part: 25[0-5] allows numbers between 250 to 255
The second part: 2[0-4]\d allows numbers between 200 to 249
The third part: 1\d\d allows numbers from 100 to 199
and the last part: [1-9]?\d allows numbers from 0 to 99, and makes sure that 00 or 01 do not get accepted, but 0, 1, etc do.

The answer is that there is an issue in klish if the range starts with 0.

Related

IF AND statement in MS WORD

I need a statement that gives a certain amount depending on the field value:
If x is in the range between 10 and 20 then the answer is 15 if not then it should go to identical statement with a different range.
I cant seem to get the first part working, this is what I got so far:
{=IF(AND(x>10,x<20)"yes","no")}
Try:
{=INT((X-1)/10)=1 \# "'Yes';;'No'"}
Alternatively:
{=AND(Val>10,Val<20) \# "'Yes';;'No'"}
To see how to do a wide range of other calculations in Word, check out my Microsoft Word Field Maths Tutorial, at:
https://www.msofficeforums.com/word/38720-microsoft-word-field-maths-tutorial.html
or:
http://www.gmayor.com/downloads.htm#Third_party
You really don't need all that circumlocution. For example:
{IF«field»> 300 125 {IF«field»> 200 100 {IF«field»> 100 75 0}}}

Regex for numerical range, show 1 and 01, but reject 0 and 00

I've puzzled a lot, but can't figure out why my regex doesn't work.
It's for an input which should accept a numerical range between 0 and 40. It should reject 0 and 00, but accept 1 or 01 and further...
Where am I wrong?
\b([1-3]?\d{1}|40)\b
[1-3]?\d{1}
matches 0 because [1-3] is optional and \d of course includes 0. Also, the {1} is a no-op - every token is matched once by definition.
You need something like this:
\b(0?[1-9]|[1-3]\d|40)\b

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

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$

Regex to validate port number

I'm using this regex (6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3} to validate port numbers. Somehow this is not working. What is wrong with this? Can anybody point me out.
What exactly do you mean by not working?
You could try something like so: ^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$ (obtained from here).
This will make sure that any given string is numeric and between the range of 0 and 65535.
Assuming your regular expression matches the same range, it is missing the start and end anchors (^ and $ respectively), so it would allow other strings besides the actual port.
Update 2 Feb 2022: Fixed the regex to reject values like 00 etc. The updated regex is sourced from the comment below. This regex can be better understood and visualized here: https://www.debuggex.com/r/jjEFZZQ34aPvCBMA
When, we search "how to validate port number" on Google we unfortunately land here
However (except if you have really no other choice...),
Regex is clearly not the way to validate a port number !
"One" (slightly better) way may be:
step 1: Convert your string into number, and return FALSE if it fails
step 2: return TRUE if your number is in [1-65535] range, and FALSE otherwise
Various reasons, why Regex is not the right way ?
Code readability (would takes few minutes to understand)
Code robustness (there are various ways to introduce a typo, a unitary test would be required)
Code flexibility (what if port number can be extended to a 64-bits number !?)
etc. ...
Number() is the function you want "123a" returns NAN
parseInt() truncates trailing letters "123a" returns 123
<input type="text" id="txtFld" onblur="if(Number(this.value)>0 && Number(this.value)<65536){alert('valid port number');}" />
jsfiddle
Here is the example I'm using to validate port settings for a firewall. The original answer will match 2 strings. I can only have 1 string match.
(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[1-9](\d){0,3})
To get: 22,24:100,333,678,100:65535 my full validation (That will only return 1 match) is
(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3})(:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}))?(,(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}){1}(:(6553[0-5]|655[0-2][0-9]|65[0-4][0-9][0-9]|6[0-4][0-9][0-9][0-9][0-9]|[1-5](\d){4}|[0-9](\d){0,3}))?)*
A more strict approach is to have a regex matching all numbers up to 5 digits
with the following string:
*(^[1-9]{1}$|^[0-9]{2,4}$|^[0-9]{3,4}$|^[1-5]{1}[0-9]{1}[0-9]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-4]{1}[0-9]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-5]{1}[0-4]{1}[0-9]{1}[0-9]{1}$|^[1-6]{1}[0-5]{1}[0-5]{1}[0-3]{1}[0-5]{1}$)*
The accpeted answer by npinti is not right. It will not allow to enter port number 1000, for example. For me, this one (not nice, I'm a beginner) works correctly:
/^((((([1-9])|([1-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9][0-9][0-9])|([1-6][0-5][0-5][0-3][0-5])))))$/
"^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$"
It will allow everything between 0-65535 inclusive.
Here is single port regex validation that excludes ports that start with 0
^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])
Here is validation for port range (ex. 1111-1111)
^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])(-([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$
link:
https://github.com/findhit/proxywrap/issues/13
Landed here as well, searching specifically for REGEX to validate port number.
I see the approved solution was not fixed yet to cover all scenarios ( eg: 007 port, and others ) and solutions from other sites not updated either (eg).
Reached same minimal solution as saber tabatabaee yazdi, that should cover the 1-65535 range properly:
^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
Enjoy !
#npinti 's answer allows leading zeros in the port number and also port 0 means pick any available port so I would exclude that so the regex becomes
^([1-9][0-9]{0,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
If you want to allow port 0 then
^(0|[1-9][0-9]{0,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
The solution:
Dim Minlenght As Integer = 1
Dim Maxlenght As Integer = 65536
Regex.IsMatch(sInput,"(^\d{0},{1}$)", "{" + Minlenght, Maxlenght + "}")
If variable is an integer between 1 and 65536 (inclusive) then...
if [[ "$port" =~ ^[0-9]+$ && $port -ge 1 && $port -le 65536 ]]; then
^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0][0-9]{1,4})|([0-9]{1,4}))$
I have tested above regrex with Junit run the for loop from 0-65535
Ex: 00001 - 65535 with leading Zeros
1 - 65535 without leading Zeros
Ex:====
(6553[0-5]) : 65530-65535
(655[0-2][0-9]) : 65500-65529
(65[0-4][0-9]{2}): 65000-65499
(6[0-4][0-9]{3}) : 60000-64999
([1-5][0-9]{4}) : 10000-59999
([0-5]{0,5}) : 00000-55555 (for leading Zeros)
([0][0-9]{1,4}) : 00000-09999 (for leading Zeros)
([0-9]{1,4}) : 0000-9999 (for leading Zeros)

RegEx for value Range from 1 - 365

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]?$