How to write a regular expression in c++ - c++

I have this string dummy_data:\m192.168.1.125\pApp and I want to extract the IP address from the given string.
I have used the following regular Expression:
\\\\m([\\d\\w\\.]+)\\\\?
This returns \m192.168.1.125, but I want only 192.168.1.125
Do you have any suggestions on how to achieve this?

This one is simple:
[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?
It only works for IPv4 addresses.
This one also worked for your string:
([0-9]{1,3}\.){3}[0-9]{1,3}
I tested both on this random page. I can not tell you how reliable they are.

Related

Extract domain from email address with 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.
#(?<=#).*

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.

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.

Searching for ip addresses using wingrep

I'm using WinGrep to search for IP Addresses in a .txt file, but i can't figure out how to get it to use a regular expression to search. does anyone have any experience with this?
If you don't need to consider IPv6, only want numeric IP addresses without port numbers, and don't need to validate the addresses (i. e., not matching illegal addresses line 321.456.299.999) then you can use
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
If you need to do any of the above things, please update your question.
What worked for me using WinGrep is the following:
[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+
I tried more sofisticated regex'es but none of them work with WinGrep
Hope it helps
Regards

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.