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

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.

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

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

Regular expression for complicated email [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
I want an regex for email that starts with numbers and letters.
My regex is
/^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
What is wrong in this? It allows ._- from start and in between. But I don't want this
Valid emails = s#gmail.com, s.p#y.com, s_p123#g.com
Invalid emails = ....s#g.com, s---g#g.com, s...#g.com, 44s..p#g.com, ----s#g.com
Does anyone know how this can be done?
So you dont want to have more than one ._- in your regex? And also it shall not start with ._- Try it this way:
^[a-zA-Z0-9]+[._-]?[a-zA-Z0-9]*#[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}
It works for your examples, although im not really sure what email-syntax you really want to test for.
Perhaps you want to use one of many pre written examples in the web (just google ;) )

Regexp to find hostname and subdomain from email address [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse email addresses for “from” and “to” fields in Ruby
Given an email address of the form root#host.sub.domain.com, what is the regular expression to get the hostname and the subdomain as two individual variables? I mean $1='host'and $2='sub'?
You help is greatly appreciated :)
EDIT I should say I'm asking about a regular expression to do this, no Ruby code. This is to be used in a postfix configuration file, so I need a regexp. I mistakenly talked about Ruby earlier. My fault, sorry for the confusion.
Non-greedy groups are your friend. They match just what you need:
^(?:.*?)#(.*?)\.(.*?)\.(?:.*?)\.(?:.*?)$