0

I would like to extract the IPv4 address only from the output below, after running ipconfig, using Regex.

    Connection-specific DNS Suffix  . : domain.local
    Link-local IPv6 Address . . . . . : fe80::49b6:17f6:f059:1c3d%12
    IPv4 Address. . . . . . . . . . . : 192.168.2.180
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 192.168.2.254

Therefore I would like to extract the '192.168.2.180' address only.

3
  • If you're in an environment where you can run regular expressions, chances are that there's a more direct way to obtain this information (probably in a neater/more "ready for whatever the next step of this process is" form) than by running the ipconfig command. Commented Jun 25, 2013 at 12:48
  • i connect remotely to a machine, via telnet and automatically run the IPCONFIG command. All i need is to extract the ip address. Commented Jun 25, 2013 at 12:51
  • By the time that the telnet command has connected to the remote machine, it's already had to find out the IP address. Commented Jun 25, 2013 at 12:52

2 Answers 2

4

^ *IPv4 Address[.\s]*:\s*([\d.]+)\s*$

It does not do any sanity checks on the IP address.

Sign up to request clarification or add additional context in comments.

2 Comments

@Pharaon can you assist with a Regex that gives me just the IP address, without the string of "IPv4 Address" ?
The regex pattern already groups the IP address. Check group #1 for its contents.
1

There are better ways to parse IP addresses, but this one is simple and grabs only what you need:

http://rubular.com/r/PxEX4Lk56w

\s+IPv4 Address.*: ([\d\.]+)

4 Comments

can you assist with a Regex that gives me just the IP address, without the string of "IPv4 Address" ?
What do you mean? Are you trying to parse a different string now? This and Pharoah's regex will both capture the IP address to a group, which is what you want.
using Pharoah's i get : "IPv4 Address. . . . . . . . . . . : 192.168.2.180" all i actually need is the address itself "192.168.2.180
...no. What language are you using? You need to use capture groups to find the IP address alone. For example, in python this would be: re.match(string, regex).group(1).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.