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'
^ ^
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.
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"
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]);
Related Topics
Get Affected Rows on Executenonquery
C# 8 Switch Expression with Multiple Cases with Same Result
C# Linq Where Date Between 2 Dates
How to Determine If an Event Is Already Subscribed
How to Log a User Out When They Close Their Browser or Tab in ASP.NET MVC
How to Define Multiple Names for Xmlelement Field
How to Put Text on Progressbar
Identifying Nhibernate Proxy Classes
Best Way to Dynamically Set an Appender File Path
Changing the View for a Viewmodel
How to Concatenate Two System.Io.Stream Instances into One
Combine Two Images into One New Image
C# Date Formatting Is Losing Slash Separators
Launch a Process Under Another User's Credentials