2

目前我得到了这个:

/^[0-9]{1,3}$/\.{1}/^[0-9]{1,3}$/\.{1}^[0-9]{1,3}$/

它应该接受以下内容:

  • 0.0.0
  • 00.00.00
  • 000.000.000

它不应该接受:

  • 000.000.0000
  • 0000.000.000
  • 000.0000.000
  • .0.0
  • 0..0
  • 等等
4

4 回答 4

2

尝试这个:

^(0+)(\.\1){2}$

意义:

^        # the start of the input
(0+)     # match one ore more `0`'s and store it in group 1
(        # start group 2
  \.\1   #   match a literal `.` and match whatever group 1 matched
){2}     # end group 2 and repeat it twice
$        # the end of the input

如果您只需要最多 3 个数字的 3 个块,请执行以下操作:

^\d{1,3}(\.\d{1,3}){2}$

或者

^\d{1,3}\.\d{1,3}\.\d{1,3}$

请注意,这也将 match "777.888.999",但使用 regex 验证数字范围并不是 regex 的用途。

于 2012-05-03T11:27:22.017 回答
1
^([2][5][0-5]|[2][0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])[.]([2][5][0-5]|[2][0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9]).([2][5][0-5]|[2][0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])$

我针对以下内容进行测试:

0.0.0
1.3.2
2.6.4
3.9.6
4.12.8
5.15.10
6.18.12
7.21.14
8.24.16
9.27.18
10.30.20
11.33.22
12.36.24
13.39.26
14.42.28
15.45.30
16.48.32
17.51.34
18.54.36
19.57.38
20.60.40
21.63.42
22.66.44
23.69.46
24.72.48
25.75.50
26.78.52
27.81.54
28.84.56
29.87.58
30.90.60
31.93.62
32.96.64
33.99.66
34.102.68
35.105.70
36.108.72
37.111.74
38.114.76
39.117.78
40.120.80
41.123.82
42.126.84
43.129.86
44.132.88
45.135.90
46.138.92
47.141.94
48.144.96
49.147.98
50.150.100
51.153.102
52.156.104
53.159.106
54.162.108
55.165.110
56.168.112
57.171.114
58.174.116
59.177.118
60.180.120
61.183.122
62.186.124
63.189.126
64.192.128
65.195.130
66.198.132
67.201.134
68.204.136
69.207.138
70.210.140
71.213.142
72.216.144
73.219.146
74.222.148
75.225.150
76.228.152
77.231.154
78.234.156
79.237.158
80.240.160
81.243.162
82.246.164
83.249.166
84.252.168
255.255.255
1.2.3
87.261.174
88.264.176
89.267.178
90.270.180
91.273.182
92.276.184
93.279.186
94.282.188
95.285.190
96.288.192
97.291.194
98.294.196
99.297.198
100.300.200
101.303.202
102.306.204
103.309.206
104.312.208
105.315.210
106.318.212
107.321.214
108.324.216
109.327.218
110.330.220
111.333.222
112.336.224
113.339.226
114.342.228
115.345.230
116.348.232
117.351.234
118.354.236
119.357.238
120.360.240
121.363.242
122.366.244
123.369.246
124.372.248
125.375.250
126.378.252
127.381.254
128.384.256
129.387.258
130.390.260
131.393.262
132.396.264
133.399.266
134.402.268
135.405.270
136.408.272
137.411.274
138.414.276
139.417.278
140.420.280
141.423.282
142.426.284
143.429.286
144.432.288
145.435.290
146.438.292
147.441.294
148.444.296
149.447.298
150.450.300
151.453.302
152.456.304
153.459.306
154.462.308
155.465.310
156.468.312
157.471.314
158.474.316
159.477.318
160.480.320
161.483.322
162.486.324
163.489.326
164.492.328
165.495.330
166.498.332
167.501.334
168.504.336
169.507.338
170.510.340
171.513.342
172.516.344
173.519.346
174.522.348
175.525.350
176.528.352
177.531.354
178.534.356
179.537.358
180.540.360
181.543.362
182.546.364
183.549.366
184.552.368
185.555.370
186.558.372
187.561.374
188.564.376
189.567.378
190.570.380
191.573.382
192.576.384
193.579.386
194.582.388
195.585.390
196.588.392
197.591.394
198.594.396
199.597.398
200.600.400
201.603.402
202.606.404
203.609.406
204.612.408
205.615.410
206.618.412
207.621.414
208.624.416
209.627.418
210.630.420
211.633.422
212.636.424
213.639.426
214.642.428
215.645.430
216.648.432
217.651.434
218.654.436
219.657.438
220.660.440
221.663.442
222.666.444
223.669.446
224.672.448
225.675.450
226.678.452
227.681.454
228.684.456
229.687.458
230.690.460
231.693.462
232.696.464
233.699.466
234.702.468
235.705.470
236.708.472
237.711.474
238.714.476
239.717.478
240.720.480
241.723.482
242.726.484
243.729.486
244.732.488
245.735.490
246.738.492
247.741.494
248.744.496
249.747.498
250.750.500
251.753.502
252.756.504
253.759.506
254.762.508
255.765.510
256.768.512
257.771.514
258.774.516
259.777.518
260.780.520
261.783.522
262.786.524
263.789.526
264.792.528
265.795.530
266.798.532
267.801.534
268.804.536
269.807.538
270.810.540
271.813.542
225.816.5441
273.819.546
274.8221.548
2725.8252.550
于 2012-05-03T19:29:32.097 回答
1

你的模式有几个问题

 /^[0-9]{1,3}$/.{1}/^[0-9]{1,3}$/.{1}^[0-9]{1,3}$/
  1. ^是字符串开头的锚点,$结尾的锚点。

  2. 要转义特殊字符,请使用反斜杠

  3. {1}是多余的。

所以我们在这里结束:

^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$

在 Regexr 上查看

但这仅检查模式!它也将接受例如 000、256 和 999,但匹配某个数字范围很困难,并且需要更复杂的正则表达式。

检查范围的模式看起来像这样(我只做了一个快速测试!)

^(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(?:.(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){2}$

在 Regexr 上查看

我不建议这样做!

于 2012-05-03T11:48:49.310 回答
-2

如果要匹配 RGB 值,请使用范围:

^[0-256]{1,3}(\.[0-256]{1,3}){2}$
于 2012-05-03T14:21:41.120 回答