0

Possible Duplicate:
Regular expression to match DNS hostname or IP Address?

I need regular expression to get the IP address from email header. There are lot's of header under received Section.

But I want to read ipaddress only from the line which have pattern like this.

Received: from unknown 124.253.91.178 by rediffmail.com via HTTP; 11 Jan 2013 10:10:35 -0000

I tried the below regex, But it returns all the ip address of email header text.

 preg_match_all("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $this->email, $matches); 

I want to match Received: and from word and ip Address in the one line. Ip address can be IPv6 or IPv4

Cœur
  • 37,241
  • 25
  • 195
  • 267
Navneet Singh
  • 1,218
  • 11
  • 17

1 Answers1

1

You can try with following regex:

/^Received: .*?(\d{1,3}(?:\.\d{1,3}){3}))/

Regex for both IPv4 and IPv6:

/^Received: .*?((?:\d{1,3}(?:\.\d{1,3}){3})|([a-z0-9]{4}(?::[a-z0-9]{4}){7}))/
hsz
  • 148,279
  • 62
  • 259
  • 315