$re = '/^(((?!25?[6-9]|0)[12]?\d|2)?\d\.?\b){4}$/m';
$str = '30.168.1.255.1
127.1
192.168.1.256
-1.2.3.4
1.1.1.1.
.1.1.1.1
01.1.1.1
3...3
1212.1.1.1
2121.1.1.1
0.0.0.0
1.7.3.5
2.14.6.10
3.21.9.15
4.28.12.20
5.35.15.25
6.42.18.30
7.49.21.35
8.56.24.40
9.63.27.45
10.70.30.50
11.77.33.55
12.84.36.60
13.91.39.65
14.98.42.70
15.105.45.75
16.112.48.80
17.119.51.85
18.126.54.90
19.133.57.95
20.140.60.100
21.147.63.105
22.154.66.110
23.161.69.115
24.168.72.120
25.175.75.125
26.182.78.130
27.189.81.135
28.196.84.140
29.203.87.145
30.210.90.150
31.217.93.155
32.224.96.160
33.231.99.165
34.238.102.170
35.245.105.175
36.252.108.180
37.3.111.185
38.10.114.190
39.17.117.195
40.24.120.200
41.31.123.205
42.38.126.210
43.45.129.215
44.52.132.220
45.59.135.225
46.66.138.230
47.73.141.235
48.80.144.240
49.87.147.245
50.94.150.250
51.101.153.255
52.108.156.4
53.115.159.9
54.122.162.14
55.129.165.19
56.136.168.24
57.143.171.29
58.150.174.34
59.157.177.39
60.164.180.44
61.171.183.49
62.178.186.54
63.185.189.59
64.192.192.64
65.199.195.69
66.206.198.74
67.213.201.79
68.220.204.84
69.227.207.89
70.234.210.94
71.241.213.99
72.248.216.104
73.255.219.109
74.6.222.114
75.13.225.119
76.20.228.124
77.27.231.129
78.34.234.134
79.41.237.139
80.48.240.144
81.55.243.149
82.62.246.154
83.69.249.159
84.76.252.164
85.83.255.169
86.90.2.174
87.97.5.179
88.104.8.184
89.111.11.189
90.118.14.194
91.125.17.199
92.132.20.204
93.139.23.209
94.146.26.214
95.153.29.219
96.160.32.224
97.167.35.229
98.174.38.234
99.181.41.239
100.188.44.244
101.195.47.249
102.202.50.254
103.209.53.3
104.216.56.8
105.223.59.13
106.230.62.18
107.237.65.23
108.244.68.28
109.251.71.33
110.2.74.38
111.9.77.43
112.16.80.48
113.23.83.53
114.30.86.58
115.37.89.63
116.44.92.68
117.51.95.73
118.58.98.78
119.65.101.83
120.72.104.88
121.79.107.93
122.86.110.98
123.93.113.103
124.100.116.108
125.107.119.113
126.114.122.118
127.121.125.123
128.128.128.128
129.135.131.133
130.142.134.138
131.149.137.143
132.156.140.148
133.163.143.153
134.170.146.158
135.177.149.163
136.184.152.168
137.191.155.173
138.198.158.178
139.205.161.183
140.212.164.188
141.219.167.193
142.226.170.198
143.233.173.203
144.240.176.208
145.247.179.213
146.254.182.218
147.5.185.223
148.12.188.228
149.19.191.233
150.26.194.238
151.33.197.243
152.40.200.248
153.47.203.253
154.54.206.2
155.61.209.7
156.68.212.12
157.75.215.17
158.82.218.22
159.89.221.27
160.96.224.32
161.103.227.37
162.110.230.42
163.117.233.47
164.124.236.52
165.131.239.57
166.138.242.62
167.145.245.67
168.152.248.72
169.159.251.77
170.166.254.82
171.173.1.87
172.180.4.92
173.187.7.97
174.194.10.102
175.201.13.107
176.208.16.112
177.215.19.117
178.222.22.122
179.229.25.127
180.236.28.132
181.243.31.137
182.250.34.142
183.1.37.147
184.8.40.152
185.15.43.157
186.22.46.162
187.29.49.167
188.36.52.172
189.43.55.177
190.50.58.182
191.57.61.187
192.64.64.192
193.71.67.197
194.78.70.202
195.85.73.207
196.92.76.212
197.99.79.217
198.106.82.222
199.113.85.227
200.120.88.232
201.127.91.237
202.134.94.242
203.141.97.247
204.148.100.252
205.155.103.1
206.162.106.6
207.169.109.11
208.176.112.16
209.183.115.21
210.190.118.26
211.197.121.31
212.204.124.36
213.211.127.41
214.218.130.46
215.225.133.51
216.232.136.56
217.239.139.61
218.246.142.66
219.253.145.71
220.4.148.76
221.11.151.81
222.18.154.86
223.25.157.91
224.32.160.96
225.39.163.101
226.46.166.106
227.53.169.111
228.60.172.116
229.67.175.121
230.74.178.126
231.81.181.131
232.88.184.136
233.95.187.141
234.102.190.146
235.109.193.151
236.116.196.156
237.123.199.161
238.130.202.166
239.137.205.171
240.144.208.176
241.151.211.181
242.158.214.186
243.165.217.191
244.172.220.196
245.179.223.201
246.186.226.206
247.193.229.211
248.200.232.216
249.207.235.221
250.214.238.226
251.221.241.231
252.228.244.236
253.235.247.241
254.242.250.246
255.249.253.251
256.0.0.0
1.257.1.1
2.2.258.2
3.3.3.259
260.4.4.4
5.261.5.5
6.6.262.6
7.7.7.263
264.8.8.8
9.265.9.9
10.10.266.10
11.11.11.267
268.12.12.12
13.269.13.13
14.14.270.14
15.15.15.271
272.0.0.0
1.273.1.1
2.2.274.2
3.3.3.275
276.4.4.4
5.277.5.5
6.6.278.6
7.7.7.279
280.8.8.8
9.281.9.9
10.10.282.10
11.11.11.283
284.12.12.12
13.285.13.13
14.14.286.14
15.15.15.287
288.0.0.0
1.289.1.1
2.2.290.2
3.3.3.291
292.4.4.4
5.293.5.5
6.6.294.6
7.7.7.295
296.8.8.8
9.297.9.9
10.10.298.10
11.11.11.299
300.12.12.12
13.301.13.13
14.14.302.14
15.15.15.303
304.0.0.0
1.305.1.1
2.2.306.2
3.3.3.307
308.4.4.4
5.309.5.5
6.6.310.6
7.7.7.311
312.8.8.8
9.313.9.9
10.10.314.10
11.11.11.315
316.12.12.12
13.317.13.13
14.14.318.14
15.15.15.319';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php