Extract domain from email address with Regex - regex

I'm learning regular expressions and I'm having trouble extracting the domain from the email address. I have an email address: example#gmail.com. I need to use a regular expression to extract #gmail (along with the # symbol). I should end up only getting example. I've already tried this:
your text#(\w+)
and this
your text(?<=#)[^.]+(?=.).*
but those expressions didn't work properly. I'd appreciate your help.

I just tried a simple look behind - #(?<=#).* it will match #google.com you can also group the entire expression and can change it according to single and multi-line matches.
#(?<=#).*

Related

Regular expression to match valid IP addresses over multiple lines

How can I construct a regular expression that matches a valid IP address [1-255] and is able to be multiline and allow whitespace? The values will be typed out and submitted like this:
10.10.10.10
100.100.100.100
192.1.1.1.1
192.158.1.38
and so on with no limit.
I have this expression that I have tweaked but only does a fraction of what I need it to do:
"^(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\/?[0-4]?[0-9]?\s?\r?\n?\.?\d)*$\b"
Something as simple as the following should do it:
\b(?<!\.)(?:(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])(?!\.)\b
Not too sure what you think you're doing with all the new line checks though, regex engines handle new lines on their own just fine. See it in action here.

How can I use the following regex to validate an email group separated by semicolon?

I have the following regex that works well for validating email address formats for a single email address. I need to use the following regex with an email input allowing multiple emails separated by semicolons.
How can the following regex be modified to achieve this?
^((([A-Za-z]|\d|[#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([A-Za-z]|\d|[#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([A-Za-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([A-Za-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$
((([A-Za-z]|\d|[#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([A-Za-z]|\d|[#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([A-Za-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([A-Za-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([A-Za-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([A-Za-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?;
This appears to be working for me but I can't be positive since I don't have any sample data. Let me know if this works for you.

Regex to extract multiple matching string

I am trying to extract HostName and Email address from text file using regex. These fields are optional. It is possible that in some cases only one field is available.
For example: if email address is not available is should extract only hostname and vice versa.
I got nearly working regex. Just not working for single case. see the regex in below link.
regex: ^.*(MAIL:(?<EMAIL>.*))(HOST:(?<HOSTNAME>.*))?
https://regex101.com/r/SDOcIR/1
Note: I am not looking this for specific language.
Thanks
Try Regex: (?:MAIL:(?<EMAIL>[^ ]+))|(?:HOST:(?<HOSTNAME>.*))
Demo

Regex to match domain name (but not TLD)

I know there are tons of domain matching regular expressions floating around, but couldn’t find one to answer my particular question. I’m looking for a regular expression that will match only a URL’s domain name, but nothing else (not even the TLD). It doesn’t need to validate the domain.
So given the sample below:
https://www.orchardsoft.com
https://www.horizon-lims.com/contact/us
https://www.quartzy.com
https://qbench.net
https://www.xifin.com
...the regular expression needs to match for the following:
Orchardshot
Horizon-lims
Quartz
QBench
Xifin
The regular expression I'm starting with is this: (.|//(\w+.)+
Is anyone able to point me in the right direction?
As long as you declare the possible TLDs (here: .com.tr, .com, .net), you can use this regex:
([\w-]+)(?=\.(?:com\.tr|com|net))
In fact, an FQDN has a hierarchical structure which makes it impossible to always analyze it correctly with a regex. It would fail (match twice) for entries that contain a TLD in its path like https://www.example.com/a.combination.

Regular expression that does not contain 'auto#'

I'm working with a system that processes email addresses and need to tell it to not process email addresses that contain 'auto#' using a regular expression.
Example email addresses:
us.group-email-name.auto#somedomain.com
us.group-email-name#somedomain.com
The regex should only match the second and not the first.
Is this possible?
Thanks in advance for your time!
If you don't have to validate the email addresses and there is no way to do it with a quick built-in substring finder, you can use this regex:
((?!auto#).)*
A far better option would be a simple substring finder method/function, like Java's
emailAddress.indexOf("auto#")
which returns -1 if it doesn't find a match.