How to match IP address by using 'findstr'? Or any other method of batch in windows

mts picture mts · Apr 3, 2012 · Viewed 17.5k times · Source

    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?

Answer

Joey picture Joey · Apr 3, 2012

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.