正则表达式
主页 > 网络编程 > 正则表达式 >

使用正则表达式验证域名的介绍

2024-11-20 | 佚名 | 点击:

如何使用正则表达式验证域名

简介

域名是互联网上网站的地址,每个域名由多个标签(label)组成,标签之间用点 . 分隔。域名规则有很多细节,但基本要求是:

在本文中,我们将学习如何使用正则表达式来验证域名是否合法。

正则表达式分析

我们可以使用以下正则表达式来验证域名格式是否正确:

1

^(?:[a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,}$

解释

以下是一些合法的域名,正则表达式应当能够匹配:

不合法的域名示例

以下是一些不合法的域名,正则表达式会排除:

使用 Java 正则表达式进行域名验证

在 Java 中,我们可以使用 Pattern 类和 Matcher 类来编写一个方法来验证域名。以下是 Java 实现:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class DomainValidator {

    // 域名验证正则表达式

    private static final String DOMAIN_REGEX = "^(?:[a-zA-Z0-9-]{1,63}\\.)+[a-zA-Z]{2,}$";

    // 验证域名是否合法

    public static boolean isValidDomain(String domain) {

        // 创建模式对象

        Pattern pattern = Pattern.compile(DOMAIN_REGEX);

        // 创建匹配器对象

        Matcher matcher = pattern.matcher(domain);

        // 返回是否匹配

        return matcher.matches();

    }

    // 测试方法

    public static void main(String[] args) {

        // 测试合法域名

        System.out.println(isValidDomain("example.com"));  // true

        System.out.println(isValidDomain("www.example.com"));  // true

        System.out.println(isValidDomain("subdomain.example.co"));  // true

        System.out.println(isValidDomain("my-website.example.org"));  // true

        // 测试不合法域名

        System.out.println(isValidDomain("example..com"));  // false

        System.out.println(isValidDomain("-example.com"));  // false

        System.out.println(isValidDomain("example-.com"));  // false

        System.out.println(isValidDomain(".example.com"));  // false

        System.out.println(isValidDomain("example"));  // false

        System.out.println(isValidDomain("example.c"));  // false

        System.out.println(isValidDomain("example.123"));  // false

    }

}

解释:

总结

通过本教程,你已经学会了如何使用正则表达式来验证域名格式。我们编写了一个正则表达式,确保域名符合基本的规则:每个标签只能包含字母、数字和短横线,标签长度限制在 1 到 63 个字符之间,且顶级域名必须由字母组成且长度至少为 2 个字符。

我们还使用 Java 的 Pattern 和 Matcher 类实现了一个域名验证方法,并对合法和不合法的域名进行了测试。

希望这篇教程对你有所帮助!

指定后缀域名验证

以下是从阿里云域名购买处获得的一些域名后缀:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

.ac.cn

.ah.cn

.archi

.art

.asia

.auto

.autos

.baby

.band

.beauty

.beer

.bio

.biz

.bj.cn

.black

.blue

.bond

.cab

.cafe

.car

.cars

.cash

.cc

.center

.chat

.city

.click

.cloud

.club

.cn

.co

.college

.com

.com.cn

.company

.cool

.cq.cn

.cyou

.design

.email

.fan

.fans

.fashion

.fit

.fj.cn

.fun

.fund

.fyi

.games

.gd.cn

.global

.gold

.gov.cn

.green

.group

.gs.cn

.guru

.gx.cn

.gz.cn

.ha.cn

.hair

.hb.cn

.he.cn

.hi.cn

.hk.cn

.hl.cn

.hn.cn

.homes

.host

.icu

.info

.ink

.jl.cn

.js.cn

.jx.cn

.kim

.law

.life

.live

.ln.cn

.lotto

.love

.ltd

.luxe

.makeup

.market

.mba

.me

.media

.mo.cn

.mobi

.monster

.motorcycles

.net

.net.cn

.news

.nm.cn

.nx.cn

.online

.org.cn

.organic

.pink

.plus

.poker

.press

.pro

.promo

.protection

.pub

.pw

.qh.cn

.quest

.red

.ren

.rent

.run

.sc.cn

.school

.sd.cn

.security

.sh.cn

.shop

.shopping

.show

.site

.ski

.skin

.sn.cn

.social

.space

.storage

.store

.studio

.sx.cn

.tax

.team

.tech

.technology

.theatre

.tickets

.tj.cn

.today

.top

.tv

.tw.cn

.uno

.video

.vin

.vip

.vote

.voto

.wang

.website

.wiki

.work

.world

.xin

.xj.cn

.xyz

.xz.cn

.yachts

.yn.cn

.yoga

.zj.cn

.zone

.餐厅

.佛山

.公司

.广东

.集团

.企业

.商标

.商城

.商店

.网店

.网络

.网址

.我爱你

.游戏

.娱乐

.在线

.招聘

.中国

.中文网.ac.cn

.ah.cn

.archi

.art

.asia

.auto

.autos

.baby

.band

.beauty

.beer

.bio

.biz

.bj.cn

.black

.blue

.bond

.cab

.cafe

.car

.cars

.cash

.cc

.center

.chat

.city

.click

.cloud

.club

.cn

.co

.college

.com

.com.cn

.company

.cool

.cq.cn

.cyou

.design

.email

.fan

.fans

.fashion

.fit

.fj.cn

.fun

.fund

.fyi

.games

.gd.cn

.global

.gold

.gov.cn

.green

.group

.gs.cn

.guru

.gx.cn

.gz.cn

.ha.cn

.hair

.hb.cn

.he.cn

.hi.cn

.hk.cn

.hl.cn

.hn.cn

.homes

.host

.icu

.info

.ink

.jl.cn

.js.cn

.jx.cn

.kim

.law

.life

.live

.ln.cn

.lotto

.love

.ltd

.luxe

.makeup

.market

.mba

.me

.media

.mo.cn

.mobi

.monster

.motorcycles

.net

.net.cn

.news

.nm.cn

.nx.cn

.online

.org.cn

.organic

.pink

.plus

.poker

.press

.pro

.promo

.protection

.pub

.pw

.qh.cn

.quest

.red

.ren

.rent

.run

.sc.cn

.school

.sd.cn

.security

.sh.cn

.shop

.shopping

.show

.site

.ski

.skin

.sn.cn

.social

.space

.storage

.store

.studio

.sx.cn

.tax

.team

.tech

.technology

.theatre

.tickets

.tj.cn

.today

.top

.tv

.tw.cn

.uno

.video

.vin

.vip

.vote

.voto

.wang

.website

.wiki

.work

.world

.xin

.xj.cn

.xyz

.xz.cn

.yachts

.yn.cn

.yoga

.zj.cn

.zone

.餐厅

.佛山

.公司

.广东

.集团

.企业

.商标

.商城

.商店

.网店

.网络

.网址

.我爱你

.游戏

.娱乐

.在线

.招聘

.中国

.中文网

要求在一段文本中,用正则表达式匹配出包含我给的后缀域名的数据,正则表达式如下:

1

(?:[a-zA-Z0-9-]+\.)+(?:ac\.cn|ah\.cn|archi|art|asia|auto|autos|baby|band|beauty|beer|bio|biz|bj\.cn|black|blue|bond|cab|cafe|car|cars|cash|cc|center|chat|city|click|cloud|club|cn|co|college|com|com\.cn|company|cool|cq\.cn|cyou|design|email|fan|fans|fashion|fit|fj\.cn|fun|fund|fyi|games|gd\.cn|global|gold|gov\.cn|green|group|gs\.cn|guru|gx\.cn|gz\.cn|ha\.cn|hair|hb\.cn|he\.cn|hi\.cn|hk\.cn|hl\.cn|hn\.cn|homes|host|icu|info|ink|jl\.cn|js\.cn|jx\.cn|kim|law|life|live|ln\.cn|lotto|love|ltd|luxe|makeup|market|mba|me|media|mo\.cn|mobi|monster|motorcycles|net|net\.cn|news|nm\.cn|nx\.cn|online|org\.cn|organic|pink|plus|poker|press|pro|promo|protection|pub|pw|qh\.cn|quest|red|ren|rent|run|sc\.cn|school|sd\.cn|security|sh\.cn|shop|shopping|show|site|ski|skin|sn\.cn|social|space|storage|store|studio|sx\.cn|tax|team|tech|technology|theatre|tickets|tj\.cn|today|top|tv|tw\.cn|uno|video|vin|vip|vote|voto|wang|website|wiki|work|world|xin|xj\.cn|xyz|xz\.cn|yachts|yn\.cn|yoga|zj\.cn|zone|餐厅|佛山|公司|广东|集团|企业|商标|商城|商店|网店|网络|网址|我爱你|游戏|娱乐|在线|招聘|中国|中文网)

测试数据如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

www.example.com

www.test.ac.cn

example.企业

mywebsite.中国

www.example.ac.cn

test.ah.cn

mywebsite.archi

site.com

invalidwebsite.xyz

example.bj.cn

example.com.cn

random.website

/zuankeba/3894433.html

效果如下:

原文链接:
相关文章
最新更新