Regex to find a range in an ip address - regex

For example, on my cisco switch I want to find all results with a second octet between 63-255.
So first octet 10.
Second octet 64 - 255
Third and fourth octet can be any
So it should like this 10.[64 - 255 ]..
This where I got to
10 \?\ ..* \ ..*
Thanks

The following regex does what you want (mandatory regex101 link):
10\.(6[4-9]|[7-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(1?\d\d?|2[0-4]\d|25[0-5])\.(1?\d\d?|2[0-4]\d|25[0-5])
It may look complex (mostly because of its length), but it's actually straightforward. Here's a breakdown:
10 matches the first octet which is supposed to be 10
\. matches a dot. Same goes for the other \.s
(6[4-9]|[7-9]\d|1\d\d|2[0-4]\d|25[0-5]) matches the second octet, making sure it's between 64 and 255 (inclusive) this way:
Match a 6 followed by a 4, 5, 6, 7, 8 or 9 6[4-9] (numbers between 64 and 69), or
Match a 7, 8 or 9 followed by a digit [7-9]\d (numbers between 70 and 99), or
Match a 1 followed by two digits 1\d\d (numbers between 100 and 199), or
Match a 2 followed by a 0, 1, 2, 3 or 4, which is then followed by a digit 2[0-4]\d (numbers between 200 and 249), or
Match a 2 followed by a 5 followed by a 0, 1, 2, 3, 4 or 5 25[0-5] (numbers between 250 and 255)
The first (1?\d\d?|2[0-4]\d|25[0-5]) matches the third octet (which can be anything between 0 and 255) this way:
Match a digit optionally preceded by a 1 and optionally followed by another digit 1?\d\d? (numbers between 0 and 199), or
Match a 2 followed by a 0, 1, 2, 3 or 4, which is then followed by a digit 2[0-4]\d (numbers between 200 and 249), or
Match a 2 followed by a 5 followed by a 0, 1, 2, 3, 4 or 5 25[0-5] (numbers between 250 and 255)
The second (1?\d\d?|2[0-4]\d|25[0-5]) matches the fourth octet using the same logic

Related

Is there anyway I can avoid to match a 9 digit number from the following rules with the regex i have?

I have the below regex which follows the following rules,
(?<!x)(?=(?:[._ –-]*\d){9})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
Rules:
9 digit Order numbers should not get detected if 'X or x' precedes
the number. (WORKING FINE)
9 digit numbers, Non-numeric characters or whitespaces (up to 3) in
between numbers should also get matched. (WORKING FINE)
Below is regex demo which shows it matches the numbers with the above rules.
https://regex101.com/r/mrGcvp/1
Now, the regex pattern should not match the 9 digit numbers following the above rules if it comes under the below rules of exclusion.
Rules of exclusion,
The number should not be matched at all for the following rules.
If the number beginning with the number “9”
If the number “666” in positions 1 – 3.
If the number “000” in positions 1 – 3.
If the number “00” in positions 4 – 5.
if the number “0000” in positions 6 – 9
You can use
(?<!x)(?=(?:[._ –-]*\d){9})(?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3})\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
See the regex demo.
The added part is (?!9|66\D*6|00\D*0|(?:\d\D*){3}0\D*0|(?:\d\D*){5}0(?:\D*0){3}) and it fails the match if, immediately to the right of the current location, right after the (?<!x) and (?=(?:[._ –-]*\d){9}) checks, there is
9| - a 9 digit, or
66\D*6| - 66, zero or more non-digits, 6, or
00\D*0| - 00, zero or more non-digits, 0, or
(?:\d\D*){3}0\D*0| - three occurrences of a digit and then zero or more non-digits, and then a 0, zero or more non-digits, 0, or
(?:\d\D*){5}0(?:\D*0){3}) - five occurrences of a digit and zero or more non-digits, 0, and then three occurrences of zero or more non-digits followed with a 0 char.
Note I used \D* instead of [._ –-]* that should be enough here, but if you want to make it more precise, you may replace each \D* with [._ –-]* .
You may use this regex:
(?<![xX])(?=(?:[._ –-]*\d){9})(?!9|666|000|.{3}00|.{5}0000)\d{2,}[._ –-]*\d{2,}[._ –-]*\d{2,}
RegEx Demo
To enforce all rule we have a negative lookahead:
(?!9|666|000|.{3}00|.{5}0000)
That does following:
9: Doesn't start with 9
666: Doesn't start with 666
000: Doesn't start with 000
.{3}00: Doesn't allow 00 in position 4-5
.{5}0000: Doesn't allow 0000 in position 6-9

regex to match any number which is greater than 5

I need to match fail counts greater than 5.
string="""fail_count 7
fail_count 8
fail_count 9
fail count 7
fail_count 71
fail_count 23
"""
match = re.search(r'fail(\s|\_)count\s[5-9]', string)
if match:
print match.group()
I am able to match up to 9, but if I increase the range to 999 it doesn't work.
5-9 or at least 2 digits
'([5-9]|\d{2,})'
or to match the whole numbre when it starts by 5-9.
5-9 followed by any number of digits or at least 2 digits
'([5-9]\d*|\d{2,})'
Maybe this regex solution can help
fail(\s|\_)count\s([0-9]{2,}|[5-9]{1})
see on regex101

Extract number from string with regex in groovy

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]

Regex: Match a malformed date

I'm trying to grab the date (without time) from the following OCR'd strings:
04.10.2015, in USD
04.10.20 15, in EUR
04,1 0.2015, in XYZ
1 1. 10.2 01 5, in XYZ
0 1.11.201 5 12:30
1 1,0 3, 2 0 1 5 1 2:3 0
With the following expression I can catch the dates, but I can't skip the "12" hours:
([\d\s]{2,}(?:\.|,)[\d\s]{2,}(?:\.|,)[\d\s]{4,})
How can I make it work? In plain English, how can I make the last part stop once it has found 4 digits in a mix of digits and spaces/tabs?
By catching the first 8 digits on a line, you will get your date.
\D is any non-digit charater
\d is a digit character
(?:...) is a group that will be ignored
^\D* is used to ignore the beginning of the line until we get a digit
We match 8 times a digits followed by any non-numerics characters, starting with first digit found.
import re
p = re.compile(ur'^\D*((?:\d\D*?){8})', re.MULTILINE)
test_str = u"""04.10.2015, in USD
04.10.20 15, in EUR
04,1 0.2015, in XYZ
1 1. 10.2 01 5, in XYZ
0 1.11.201 5 12:30
1 1,0 3, 2 0 1 5 1 2:3 0
"""
print re.findall(p, test_str)
Have a test over here: https://regex101.com/r/eQ8zJ9/4
You can then filter out any non digits to get the date:
from datetime import datetime
for s in re.findall(p, test_str):
digits = re.sub(ur'\D', '', s)
print datetime.strptime(digits, '%d%m%Y')
You can also try with:
((?:\d\s*){2})[,.-]((?:\s*\d\s*){2})[,.-]((?:\s*\d){4})
DEMO
which is not restricted by beginning of a line. Also it match is there is one of choosen delimiters beetwen numbers, like ,, . or -. As there could be more 8-digits chaotic number sequences in such formatted text.
The other answer is nice and short, but if the delimiters are of importance:
((?:(?:\d\s*){2}[.,]\s*){2}(?:\d\s*?){4})
The key being:
(?:\d\s*?){𝑛}
To capture 𝑛 digits with optional, but non-greedy, whitespace in-between.
I also took the liberty to shorten (?:\.|,) to [.,].

Can someone help define what this regex does?

I'm still learning regex and was hoping someone could tell me what this regex does exactly. Thank you.
\d{8,9}0101\d{3}
Breaking it apart:
\d{8,9}
That means either eight or nine digits (0-9).
0101
That means the literal string 0101
\d{3}
That means precisely three number digits.
You can use Expresso to Know more.
Youre regex means
1.Any digit of 8 or 9 repetation
2 then 0101
3 then any digit of exact 3 repetation
I would recommend to start from some source where theory could be found, later on using some tools where you can interactively check how this knowledge can be applied.
http://www.regular-expressions.info/posix.html <- This site contains information about POSIX standard for regular expressions.
Personally for testing matching I use rubular.com, but it references ruby's implementation of regexps. So it also depends on what regexp implementation you use.
In your case it is simple to answer and there should be no difference between different regexp implementations, though.
(A) \d{8,9} - a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) repeated minimum 8 to maximum 9 times
(B) 0101 - literal string 0101
(C) \d{3} - 3 then any digit of exact 3 repetation
regex does = A + B + C
This finds 8 or 9 digits (numbers 0-9), followed by 0101 followed by exactly three digits...
(You should have been able to figure that out by searching!)
Autopsy:
\d{8,9} - a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) repeated 8 to 9 times
0101 - a literal string of the characters 0101
\d{3} - a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) repeated exactly 3 times
Note: repeated doesn't mean "the same character" but anything in the match. That means that "repeated exactly 3 times" for \d could be 111, 123, 989 etc.