Regex search string with a not includes [duplicate] - regex

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)

Related

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

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

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.

Regex: matching domain.com and any subdomains to domain.com? [duplicate]

This question already has answers here:
Match exact string
(3 answers)
Closed 2 years ago.
Hi folks I've been at this for 2 hours and figured there has to be someone out there with a better solution?
In this regex case, I need to match all domains and subdomains from bbc. Eg:
bbc.com
subdomain.bbc.co.uk
But not sites such as
another-bbc.com
anotherbbc.com
bbcmore.com
I need to keep the "bbc" part flexible so I can use other domains in future such as facebook.
The closest Ive got to is (.*\.)?bbc\..* but it's matching anotherbbc.
Please can you help me? Would appreciate this.
The following one should suit your needs:
^([^.\r\n]+\.)*(bbc\.com|bbc\.co\.uk)$
Allows any amount of subdomains for the hardcoded domains.
For this specific case, you could replace (bbc\.com|bbc\.co\.uk) with bbc\.(com|co\.uk), but if you need other domains as well (e.g. like you said, facebook.com), I believe it will be more maintainable to hardcode them entirely.
See https://regex101.com/r/PnMYpY/1.

How to create capturing groups in regular expressions? [duplicate]

This question already has answers here:
How do I regex match with grouping with unknown number of groups
(6 answers)
Closed 3 years ago.
I'm trying to use Regex to classify groups of data dependent on how many sectors are within an array. E.g.:
group = One journey
group|group = Two journeys
group|group|group = Three journeys
Could someone tell me the best practice way to do this please?
EDIT: Apologies but I'm pretty new to RegEx and still trying to work things out. I don't know which language I'm using but the tool I'm building these into is Adobe Analytics - using the Classification Rule Builder.
Also, this question has been marked as duplicate but I can't say I found the other thread particularly helpful.
I've also tried experimenting using Regex101 but still can't get my head around this. Thanks.
For such a case you need to capture what you want to match inside some block that would depend on the language you are using. For example, if you are using Python you can use:
(\w+)
This regex will allow you to capture and count every repetition of word characters, that is [a-zA-Z0-9_], that will be able to capture all the text you have between pipes.
By the way, in order to test your regex and to do some basic training and trial-error approach you can use tools like this one.

Matching multiple occurrences with regex [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I'm building an extractor in Graylog to pull tac_plus syslog data.
I have a log:
<70>Oct 13 10:10:05 auth tac_plus[17354]: 2015-10-13 10:10:05 -0500#01110.10.89.1#011jmartinez#011tty132#01110.10.1.27#011stop#011task_id=146#011timezone=CDT#011service=shell#011start_time=1444747732#011priv-lvl=15#011cmd=show running-config <cr>
I want to extract the indvidual statements between the #011 markers. I was able to get the first section, the IP with:
(?<=#011)(.*?)(?=#011)
Now I want to extract the 'jmartinez'. I'm trying:
#011.*?#011(.*)(#011)
but it matches:
jmartinez#011tty132#01110.10.1.27#011stop#011task_id=146#011timezone=CDT#011service=shell#011start_time=1444747732#011priv-lvl=15
if i do:
#011.*?#011(.*)(#011tty)
it seems to work but i'd rather it not rely on seeing #011tty because it might be something else in another message.
what about the next one? how can I extract tty132, 10.10.1.27, stop, task_id=146, etc
any help would be greatly appreciated!
The simple answer is to use a reluctant quantifier (just like your working IP capture):
#011.*?#011(.*?)#011
But I would go further and capture all groups at once, eg:
#011(.*?)#011(.*?)#011(.*?)#011(.*?)#011(.*?)#011(.*?)#011