Extracting IP addresses from text

PHP Medium regex

Task «Extracting IP addresses from text»

Write a PHP program that reads lines from standard input and extracts all valid IPv4 addresses from them. An IP address consists of four numbers from 0 to 255, separated by dots. Leading zeros are not allowed unless the number is 0. Addresses may be surrounded by spaces or punctuation marks. Output each found address on a separate line in the order they appear in the text. If no addresses are found, output an empty line.

Input Format

Several lines of text, each up to 1000 characters long. The total number of lines does not exceed 100.

Output Format

Each found IPv4 address on a separate line in the order of appearance. If there are no addresses, output an empty line.

Examples

Example 1

INPUT
Connect to 172.16.0.1 and 192.168.0.255
OUTPUT
172.16.0.1 192.168.0.255

Example 2

INPUT
Invalid addresses: 256.1.2.3, 1.2.3.4.5, 0.0.0.0
OUTPUT
0.0.0.0

Example 3

INPUT
Invalid: 999.1.2.3 but valid: 1.2.3.4
OUTPUT
1.2.3.4

Example 4

INPUT
No addresses here!
OUTPUT
main.php