Validating IPv4 addresses with regexp
You've already got a working answer but just in case you are curious what was wrong with your original approach, the answer is that you need parentheses around your alternation otherwise the (\.|$)
is only required if the number is less than 200.
'\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b'
^ ^
Using a RegEx to match IP addresses
You have to modify your regex in the following way
pat = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
that's because .
is a wildcard that stands for "every character"
javascript regular expression to check for IP addresses
The regex you've got already has several problems:
Firstly, it contains dots. In regex, a dot means "match any character", where you need to match just an actual dot. For this, you need to escape it, so put a back-slash in front of the dots.
Secondly, but you're matching any three digits in each section. This means you'll match any number between 0 and 999, which obviously contains a lot of invalid IP address numbers.
This can be solved by making the number matching more complex; there are other answers on this site which explain how to do that, but frankly it's not worth the effort -- in my opinion, you'd be much better off splitting the string by the dots, and then just validating the four blocks as numeric integer ranges -- ie:
if(block >= 0 && block <= 255) {....}
Hope that helps.
Regex for find All ip address except IP address starts with 172
.
in a regex is character that matches everything. To use it in this context, you must escape it.
Also to limit it to just ip addresses that start with 172, simply hardcode it into your regex like so:
^172\.\d{1,3}\.\d{1,3}.\d{1,3}$
Debuggex Demo
You could then use this to filter out any matches already made.
Alternatively, if you're not starting with a list of ip addresses, you could use a negative look-ahead to grab them all straight away.
^(?!172)\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$
Debuggex Demo
Be a little careful in that this may match more than ip addresses - for example 400.660.226.602
would be captured - even though real IP4 addresses do not contain numbers higher than 255
. Perhaps this won't affect your use case - but it's something to remember.
As per the comments below, if you are searching for IP addresses anywhere in the document, rather than on their own line, use \b
instead of ^
and $
\b(?!172)\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}\b
Debuggex Demo
This would match log formats for example, which contain an ip address within the message rather than on it's own line.
[10:01:22] Connection from
10.14.242.211
established.
RegEx for an IP Address
The [
shouldn't be at the start of your pattern. Also, you probably want to use Matches(...)
.
Try:
String input = @"var product_pic_fn=;var firmware_ver='20.02.024';var wan_ip='92.75.120.206';if (parent.location.href != window.location.href)";
Regex ip = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
MatchCollection result = ip.Matches(input);
Console.WriteLine(result[0]);
Private IP Address Identifier in Regular Expression
I'm assuming you want to match these ranges:
127. 0.0.0 – 127.255.255.255 127.0.0.0 /8
10. 0.0.0 – 10.255.255.255 10.0.0.0 /8
172. 16.0.0 – 172. 31.255.255 172.16.0.0 /12
192.168.0.0 – 192.168.255.255 192.168.0.0 /16
You are missing some dots that would cause it to accept for example 172.169.0.0
even though this should not be accepted. I've fixed it below. Remove the new lines, it's just for readability.
(^127\.)|
(^10\.)|
(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|
(^192\.168\.)
Also note that this assumes that the IP addresses have already been validated - it accepts things like 10.foobar
.
Related Topics
Why Does a Bad Password Cause "Padding Is Invalid and Cannot Be Removed"
Edit Specific Element in Xdocument
Thread Safety of a Dictionary<Tkey, Tvalue>
Getting Date or Time Only from a Datetime Object
Get the Name of a Method Using an Expression
C# Linq Intersect/Except with One Part of Object
Static and Instance Methods with the Same Name
Create Object Instance of a Class Having Its Name in String Variable
Combine Two Images into One New Image
How to Retrieve a List of Parameters from a Stored Procedure in SQL Server
How to Generically Format a Boolean to a Yes/No String
Performance of Nested Yield in a Tree
System.Io.File.Create Locking a File
What's the Max Items in a List<T>
Catching Unhandled Exception on Separate Threads
Can You Compile C# So It Doesn't Need the .Net Framework at Runtime