Select any IP addresses except a group of ones in RegExp [duplicate] - regex

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 2 years ago.
Fairly new to this world (network engineer by carrer).
I need to scrub the configuration files for some routers and need to guarantee that certain servers are configured, but no extra servers are there.
So, let's say I have three servers:
1.1.1.1, 1.1.1.2, 1.1.1.3
and the operator adds a fourth one (2.2.2.2, or any other ip address).
When scrubbing, I need to catch this fourth and signal so it can be marked for removal.
Well, I know how to match any ip address:
(25[0-5]|2[0-4][0-9]|[01]?[0-68-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
But how to select any IP address except the ones I need? I tried negative lookahead, but I either did not understand the use of it or used it wrong, because it did nothing to me.
Any hints?

I think this answer will help you solve your problem: Regex not operator
You should use as many lookaheads as you have valid IP addresses.

I'm not sure what you're writing in, but I would do that on the return.
First pull all the ip addresses you find with your regex (which looks fine at a glance) then just not return anything if it matches one you want to ignore.

Related

Regex search string with a not includes [duplicate]

This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 2 years ago.
I have the following content...
database-host-12334-ireland
database-host-23233-london
database-host-32323-cardiff
backup-host-2323232-ireland
backup-host-3232323-london
I want to be able to search for all database-hosts and at the same time leave out the Ireland host.
I want the search to return only 2 hosts database hosts:
database-host-23233-london
database-host-32323-cardiff
I cant figure out how I can edit my regex lookup from ^database-host-[0-9].*
Any help would be appreciated, maybe its not even possible.
Thanks
UPDATE TO PREVIOUS QUESTION:
Ok a bit more complicated this time, how would you go about excluding the one database ireland host from this list?
www.regexr.com/5gln4
database-host-12334.host.zone1.eu.ireland-1
database-host-12334.host.zone1.us.newyork-1
database-host-12334.host.zone2.uk.london-1
database-host-12334.host.zone3.uk.cardiff-1
database-host-12334.host.zone3.uk.belfast-1
backup-host-12334.host.zone2.uk.london-1
backup-host-12334.host.zone3.uk.cardiff-1
backup-host-12334.host.zone3.uk.belfast-1
I only want to return the database-hosts excluding Ireland one.
You may use a negative lookahead assertion to rule out the Ireland host:
database-host-\d+-(?!ireland)\w+
Demo
If you can't use lookarounds for some reason, another way to logically achieve what you want would be to use two regex patterns, one for whitelisting database hosts, and the other for blacklisting Ireland hosts:
database-host-\d+-\w+ && !(database-host-\d+-ireland)

Replacing a certain number of characters after a match in regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I want to find any instance of
\/Network\/Cambric Corporate Center\/
and replace the next 8 characters with nothing.
So something like this
\/Network\/Cambric Corporate Center\/1164.DEV1164
Turns into this
\/Network\/Cambric Corporate Center\/1164
I'm working with a baked in replace regular expression visual programming piece, and this is my first post to here ever so please ask if more info is needed. I basically just need it to look like this
\/Network\/Cambric Corporate Center\/1164
if there is another solution without having to use replace
It is for a frequently updated mass source of data that I need to edit to make more compatible with arrays
Try (\/Network\/Cambric Corporate Center\/).{8} and replace with $1 to keep the first group but not anything else.
Here's the regex with the replacement: https://regex101.com/r/F4Y4VD/1

Regular expression matching DNS

Simplified issue (Extendable if requested): I'm running a chat, and don't want people to spam DNS's on it. Therefore, I need a regex which can check for dns's being written on the chat, and stop them. ONLY DNS's, nothing else which may resemble a dns, but could actually be a command being executed or someone just talking. (By the way, I already have normal IP's well covered)
So I found this DNS's matching regex which is the one I apply now for censoring:
[a-zA-Z0-9\-\.]+\s?(\.|dot|\(dot\)|-|;|:|,)\s(com|org|net|cz|co|uk|sk|biz|mobi|xxx|eu|me|io)\b
It works for stopping DNS's. However, it's also censoring users typing in commands such as: /email add email#mail.com email#mail.com (A command commonly typed in on my chat, which the dns regex confuses as a dns)
Or sometimes just when someone types something containing many dots, and maybe ending in one such ending as the ones within the final regex parenthesis.
Therefore, what I need is a more precise dns matching regex, which will detect DNS's, and ONLY DNS's. However, my regex knowledge is very limited. I would really appreciate help with this!

validate email addresses using a regex. [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 7 years ago.
I am trying to validate email addresses using a regex. This is what I have now ^([\w-.]+)#([\w-]+).(aero|asia|be|biz|com.ar|co.in|co.jp|co.kr|co.sg|com|com.ar|com.mx|com.sg|com.ph|co.uk|coop|de|edu|fr|gov|in|info|jobs|mil|mobi|museum|name|net|net.mx|org|ru)*$ I found many solutions using non-capturing groups but did not know why. Also can you tell me if this is the correct regex and also if there are any valid emails which are not being validated correctly and vice-versa
Don’t bother, there are many ways to validate an email address. Ever since there are internationalized domain names, there’s no point in listing TLDs. On the other hand, if you want to limit your acceptance to only a selection of domains, you’re on the right track. Regarding your regex:
You have to escape dots so they become literals: . matches almost anything, \. matches “.”
In the domain part, you use [\w-] (without dot) which won’t work for “#mail.example.com”.
You probably should take a look at the duplicate answer.
This article shows you a monstrous, yet RFC 5322 compliant regular expression, but also tells you not to use it.
I like this one: /^.+#.+\...+$/ It tests for anything, an at sign, any number of anything, a dot, anything, and any number of anything. This will suffice to check the general format of an entered email address. In all likelihood, users will make typing errors that are impossible to prevent, like typing john#hotmil.com. He won’t get your mail, but you successfully validated his address format.
In response to your comment: if you use a non-capturing group by using (?:…) instead of (…), the match won’t be captured. For instance, all email addresses have an at sign, you don’t need to capture it. Hence, (john)(?:#)(example\.com) will provide the name and the server, not the at sign. Non-capturing groups are a regex possibility, they have nothing to do with email validation.

RegEx to get numbers between periods in IP address?

Say you have an IP address: 74.125.45.100 so its A.B.C.D
Is there a way to use RegEx to get A,B,C separately?
If it is just to extract the numbers from the IP and not to validate the IP address then you could just do:
[0-9]
However, I think a simple String.Split(".") would be an easier option.
Something very simple yet ugly would work.. giving you four groups one for each octet.
(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})
([0-9]+).([0-9]+).([0-9]+).([0-9]+)
...should do it. It's no validating regex though, allows numbers beyond 255 for each part.
Here's a crazy validating one:
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
Credit to last regex goes to RegexBuddy makers.
/(\d+)\.(\d+)\.(\d+)\.(\d+)/
First port of call for regex... RegEx Library
While others have pointed out various good regexps; May I ask why you absolutely must use regular expressions for that? It will be slow and error-prone. Most platforms do have integrated IP address functionality, or provide a way to call to inet_aton.
In case someone needs a validating RegEx for (all possible) IPv4 addresses:
([^\d.]|^)([01]{0,1}\d{1,2}|2[0-5][0-5])[.]([01]{0,1}\d{1,2}|2[0-5][0-5])[.]([01]{0,1}\d{1,2}|2[0-5][0-5])[.]([01]{0,1}\d{1,2}|2[0-5][0-5])([^\d]|$)
The IP is contained in 2nd, 3rd and 4th parameters. 1st and last are not used. Those are necessary otherwise a wrong IP like:
999.1.2.3
would be catched as "99.1.2.3". I am not sure if you want to allow IP ending with a dot, e.g.
1.2.3.4.
If not, change the last part to ([^\d.]|$). I do not allow any dots in front of it though.
I still think this RegEx is a messed monster :) and a better solution would be to validate by hand using a function.