How to use regexp to find an ipaddress from a query string?

I need help with a regexp to find out the ip address which can possibly be present in a URL.

The URLs can be in any of the following form
<domain>?a=12345&d=somestring1
<domain>?c=10.10.10.100&d=somestring1
<domain>?a=12345&b=somestring1&c=10.1.2.4d=somestring2
<domain>?a=12345&c=10.1.2.4&b=somestring1&d=somestring2

The rules
1) "c=" may not always be present
2) "c=" can be anywhere in the string but not the last, last is always "d"
3) If "c=" is present I need the value of c which should be an ip address.

$ cat urls
<domain>?a=12345&d=somestring1
<domain>?c=10.10.10.100&d=somestring1
<domain>?a=12345&b=somestring1&c=10.1.2.4&d=somestring2
<domain>?a=12345&c=10.1.2.4&b=somestring1&d=somestring2
<domain>?a=12345&b=somestring1&d=somestring2&c=10.1.2.9
$ sed -n "s/.*\(c=[^&]*\).*d=.*/\1/p" urls
c=10.10.10.100
c=10.1.2.4
c=10.1.2.4

Thanks Hanson.

Is it possible to do this in tcl ? Need only ip address without the "c="

For eg,

set urlvar <one of the URLs mentioned earlier>

set ipout [regexp "s/.*\(c=[^&]*\).*d=.*/\1/p" $urlvar]

puts "$ipout"

Yes, I think you should be able to apply the logic to TCL commands, since TCL supports regular expressions. I don't know anything about the TCL regexp command, so cannot help with details. Any chance TCL can call sed?

Yes, you can get the ip address, minus the c= part, by moving c= outside of the saved group, as follows:

$ sed -n "s/.*c=\([^&]*\).*d=.*/\1/p" urls
10.10.10.100
10.1.2.4
10.1.2.4