How to match IP address by using 'findstr'? Or any other method of batch in windows
Solution 1
Since findstr
's regex support is a bit ... dated, you usually can't use most regexes you find on the web. The following matches four runs of digits, separated by dots:
ipconfig | findstr /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
However, if you're only interested in addresses and not the subnet masks, you might want to use
ipconfig | findstr /r "Address.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
And yes, if would also match things like Address: 232345.534.78678.345
which is obviously not an IP address. But usually ipconfig
doesn't spit out such strings.
Solution 2
FINDSTR is the only native batch utility that has any support for regular expressions. But the support is very limited and non-standard. The only repeat expression supported is *
. In addition, it is limited to a maximum of 15 character class terms (see What are the undocumented features and limitations of the Windows FINDSTR command?). So I don't think it is possible to develop a native batch regex that will precisely match an IP address.
You could stay within native windows utilities and use Power Shell, or you could use JScript or VBScript via the CSCRIPT command. All three have much better regex support.
Alternatively you could download any of a number of Windows ports of Unix utilities, many of them free. GnuWin32 is a good resource (includes grep): http://gnuwin32.sourceforge.net/
Solution 3
In case the input of your findstr command should not be the result of ipconfig, you could use the following line of code to look for an ip-address in a text or csv file.
findstr /r "\<[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\>" filename.txt
It adds to metacharacters to the answer of user Joey. "\<" at the beginning and ">" at the end. Even though such a string might not be very common (or very unlikely to occur) this makes sure that no string in an ip-address-like format but with an alphabetic character at the end and/or the beginning (e.g. A198.252.206.16 or 198.252.206.16A) is returned.
Solution 4
A simplistic regex string would be
(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.(1?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.(1?[0-9]?[0-9]|2[0-4][0-9])|25[0-5]
to match exactly the range of allowable IP addresses.
EDIT: Based on comments below and Regular expressions in findstr information, modify the above regex to [0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]
to match IP addresses. Apparently FINDSTR
really is that limited in regular expression interpretation.
mts
Updated on June 29, 2022Comments
-
mts almost 2 years
As the title said, I want to match ip address with batch in windows, please tell me how I can do it?
I see that "findstr" can match with regex like "[0-9]", but how can "findstr" matches it appears one to three times? -
dbenham about 12 yearsThis regex is not supported by FINDSTR, (or any other native batch utility). It should work with power shell, JScript, or VBScript.
-
mts about 12 yearsThanks. I agree that FINDSTR is really limited in regular expression interpretation. And I think "[0-2][0-9][0-9]" may just matches from "000" to "299" and it can't match like "10.10.10.10".
-
mts about 12 yearsThanks, I've never used GNU, I may try it when I'm free.
-
mts about 12 yearsThanks. About the version, I just wanna write a little batch for myself and my friend, so I'll just make sure it can work on Win7 and XP. And thanks for your answer, it does work with ipconfig.
-
peet over 10 yearsIn both examples the search pattern [0-9] is not neccessary to be (confusing) doubled without impact on functionallity.
-
Rich over 10 years@peet, it is necessary to avoid matching a string like
...
. -
peet over 10 yearswowww, you are totally right, i did't realize
"*" matches NONE too
, thought it would be at least ONE - which is not true - so i'm eating my own words and absolutely insist the opposite. thanx for Explanation. -
peet over 10 yearsBecause "?" does not work, ONE would Need to write a small piece of code
using "for" loops to verify at least 1 number and at max 3 are found for each part between at least 4 and max 4 groups divided by "." and inside the loops one could verify the first Group between "None" and "2", the second and third between "1" and "5"
! Really nobody done this by now? -
peet over 10 yearsnice thought but it does not find IP's with NOT 3 numbers like
1.2.3.4
-
peet over 10 yearsjust to be precice, the second and third between "1" and "9" while overall and combinded not more then 255 - interesting.
-
Loduwijk over 4 yearsThis works great also when you set a variable to an IP and want to verify it is in a (roughly) IP format. In my case:
echo %3 | findstr "your-regex" > %tmp%\tmp.txt
thenset /p ip<%tmp%\tmp.txt
Only sets the variable if it passes the regex. Thank you!