-75~1到100的正则表达式式

一个常用的正则表达验证类
很早以前写的一个正则表达式验证工具类,包含了一些常见的校验和支持自定义的正则表达式匹配,可以选择完全匹配,也可以获取所有匹配项。曾用它仿造eclispe下的正则表达式插件写过一个工具。因为最近突然有几个朋友都在问这方面的问题,干脆就把代码贴出来好了。02.using system.03.using system.text.04.05.namespace mon.validate06.{07. /**//// &summary&08. /// regularmatch 的摘要说明。 09. /// &/summary& 10. public class regularmatch11. {12. private string _13. private bool _14.15. /**//// &summary&16. /// 可以进行判断的类型 17. /// &/summary& 18. public enum operation19. {20. byte, sbyte, int16, int32, int64, single, double, boolean, char, decimal, datetime, date, time,21. email, url, chinaphone, chineseword, chinesepostalcode, number, stringmodel_01, stringmodel_02, wideword, narrowword, ipaddress,22. chinesemobile, chineseid23. };24.25. public regularmatch() { }26.27. 用于判断字符串是否是对应类型(默认为包含匹配)#region 用于判断字符串是否是对应类型(默认为包含匹配)28. public bool isaccordtype(string strverifystring, operation op)29. {30. return isaccordtype(strverifystring, op, false);31. } 32. #endregion 33.34. 用于判断字符串是否是对应类型(或是否包含对应类型的字符)#region 用于判断字符串是否是对应类型(或是否包含对应类型的字符)35. /**//// &summary&36. /// 用于判断字符串是否是对应类型 37. /// &/summary& 38. /// &param name=strverifystring&string,需要判断的字符串&/param& 39. /// &param name=op&operation枚举,用于选择需要进行的操作&/param& 40. /// &param name=isentirety&boolean,判断是完全匹配还是包含匹配模式(仅适用于非类型判断时)&/param& 41. /// &returns&&/returns& 42. public bool isaccordtype(string strverifystring, operation op, bool isentirety)43. {44. _string =45. _isentirety =46.47. switch (op)48. {49. case operation.byte:50. {51. return isbyte();52. }53. case operation.sbyte:54. {55. return issbyte();56. }57. case operation.int16:58. {59. return isint16();60. }61. case operation.int32:62. {63. return isint32();64. }65. case operation.int64:66. {67. return isint64();68. }69. case operation.single:70. {71. return issingle();72. }73. case operation.double:74. {75. return isdouble();76. }77. case operation.boolean:78. {79. return isboolean();80. }81. case operation.char:82. {83. return ischar();84. }85. case operation.decimal:86. {87. return isdecimal();88. }89. case operation.datetime:90. {91. return isdatetime();92. }93. case operation.date:94. {95. return isdate();96. }97. case operation.time:98. {99. return istime();100. }101. case operation.ipaddress:102. {103. return isipaddress();104. }105. case operation.chinaphone:106. {107. return ischinaphone();108. }109. case operation.chinesepostalcode:110. {111. return ischinesepostalcode();112. }113. case operation.chinesemobile:114. {115. return ischinesemobile();116. }117. case operation.email:118. {119. return isemail();120. }121. case operation.url:122. {123. return isurl();124. }125. case operation.chineseword:126. {127. return ischineseword();128. }129. case operation.number:130. {131. return isnumber();132. }133. case operation.stringmodel_01:134. {135. return isstringmodel_01();136. }137. case operation.stringmodel_02:138. {139. return isstringmodel_02();140. }141. case operation.wideword:142. {143. return iswideword();144. }145. case operation.narrowword:146. {147. return isnarrowword();148. }149. case operation.chineseid:150. {151. return ischineseid();152. }153. default:154. {155.156. }157. }158. } 159. #endregion 160.161. 具体验证方法#region 具体验证方法162.163. 是否byte类型(8 位的无符号整数): 0 和 255 之间的无符号整数#region 是否byte类型(8 位的无符号整数): 0 和 255 之间的无符号整数164. /**//// &summary&165. /// 是否byte类型(8 位的无符号整数): 0 和 255 之间的无符号整数 166. /// &/summary& 167. /// &returns&boolean&/returns& 168. protected bool isbyte()169. {170. try171. {172. byte.parse(_string);173. }174. catch175. {176.177. }178.179. } 180. #endregion 181.182. 是否sbyte类型(8 位的有符号整数): -128 到 +127 之间的整数#region 是否sbyte类型(8 位的有符号整数): -128 到 +127 之间的整数183. /**//// &summary&184. /// 是否sbyte类型(8 位的有符号整数): -128 到 +127 之间的整数 185. /// &/summary& 186. /// &returns&boolean&/returns& 187. protected bool issbyte()188. {189. try190. {191. sbyte.parse(_string);192. }193. catch194. {195.196. }197.198. } 199. #endregion 200.201. 是否int16类型(16 位的有符号整数): -32768 到 +32767 之间的有符号整数#region 是否int16类型(16 位的有符号整数): -32768 到 +32767 之间的有符号整数202. /**//// &summary&203. /// 是否int16类型(16 位的有符号整数): -32768 到 +32767 之间的有符号整数 204. /// &/summary& 205. /// &returns&boolean&/returns& 206. protected bool isint16()207. {208. try209. {210. int16.parse(_string);211. }212. catch213. {214.215. }216.217. } 218. #endregion 219.220. 是否int32类型(32 位的有符号整数):-2,147,483,648 到 +2,147,483,647 之间的有符号整数#region 是否int32类型(32 位的有符号整数):-2,147,483,648 到 +2,147,483,647 之间的有符号整数221. /**//// &summary&222. /// 是否int32类型(32 位的有符号整数):-2,147,483,648 到 +2,147,483,647 之间的有符号整数 223. /// &/summary& 224. /// &returns&boolean&/returns& 225. protected bool isint32()226. {227. try228. {229. int32.parse(_string);230. }231. catch232. {233.234. }235.236. } 237. #endregion 238.239. 是否int64类型(64 位的有符号整数): -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 之间的整数#region 是否int64类型(64 位的有符号整数): -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 之间的整数240. /**//// &summary&241. /// 是否int64类型(64 位的有符号整数): -9,223,372,036,854,775,808 到 +9,223,372,036,854,775,807 之间的整数 242. /// &/summary& 243. /// &returns&boolean&/returns& 244. protected bool isint64()245. {246. try247. {248. int64.parse(_string);249. }250. catch251. {252.253. }254.255. } 256. #endregion 257.258. 是否single类型(单精度(32 位)浮点数字): -3. 和 +3. 之间的单精度 32 位数字#region 是否single类型(单精度(32 位)浮点数字): -3. 和 +3. 之间的单精度 32 位数字259. /**//// &summary&260. /// 是否single类型(单精度(32 位)浮点数字): -3. 和 +3. 之间的单精度 32 位数字 261. /// &/summary& 262. /// &returns&boolean&/returns& 263. protected bool issingle()264. {265. try266. {267. single.parse(_string);268. }269. catch270. {271.272. }273.274. } 275. #endregion 276.277. 是否double类型(单精度(64 位)浮点数字): -1.32e308 和 +1.32e308 之间的双精度 64 位数字#region 是否double类型(单精度(64 位)浮点数字): -1.32e308 和 +1.32e308 之间的双精度 64 位数字278. /**//// &summary&279. /// 是否double类型(单精度(64 位)浮点数字): -1.32e308 和 +1.32e308 之间的双精度 64 位数字 280. /// &/summary& 281. /// &returns&boolean&/returns& 282. protected bool isdouble()283. {284. try285. {286. double.parse(_string);287. }288. catch289. {290.291. }292.293. } 294. #endregion 295.296. 是否boolean类型(布尔值):true 或 false#region 是否boolean类型(布尔值):true 或 false297. /**//// &summary&298. /// 是否double类型(单精度(64 位)浮点数字): -1.32e308 和 +1.32e308 之间的双精度 64 位数字 299. /// &/summary& 300. /// &returns&boolean&/returns& 301. protected bool isboolean()302. {303. try304. {305. boolean.parse(_string);306. }307. catch308. {309.310. }311.312. } 313. #endregion 314.315. 是否char类型(unicode(16 位)字符):该 16 位数字的值范围为从十六进制值 0x0000 到 0xffff#region 是否char类型(unicode(16 位)字符):该 16 位数字的值范围为从十六进制值 0x0000 到 0xffff316. /**//// &summary&317. /// 是否char类型(unicode(16 位)字符):该 16 位数字的值范围为从十六进制值 0x0000 到 0xffff 318. /// &/summary& 319. /// &returns&boolean&/returns& 320. protected bool ischar()321. {322. try323. {324. char.parse(_string);325. }326. catch327. {328.329. }330.331. } 332. #endregion 333.334. 是否char类型(96 位十进制值):从正 79,228,162,514,264,337,593,543,950,335 到负 79,228,162,514,264,337,593,543,950,335 之间的十进制数#region 是否char类型(96 位十进制值):从正 79,228,162,514,264,337,593,543,950,335 到负 79,228,162,514,264,337,593,543,950,335 之间的十进制数335. /**//// &summary&336. /// 是否char类型(96 位十进制值):从正 79,228,162,514,264,337,593,543,950,335 到负 79,228,162,514,264,337,593,543,950,335 之间的十进制数 337. /// &/summary& 338. /// &returns&boolean&/returns& 339. protected bool isdecimal()340. {341. try342. {343. decimal.parse(_string);344. }345. catch346. {347.348. }349.350. } 351. #endregion 352.353. 是否datetime类型(表示时间上的一刻): 范围在公元(基督纪元)0001 年 1 月 1 日午夜 12:00:00 到公元 (c.e.) 9999 年 12 月 31 日晚上 11:59:59 之间的日期和时间#region 是否datetime类型(表示时间上的一刻): 范围在公元(基督纪元)0001 年 1 月 1 日午夜 12:00:00 到公元 (c.e.) 9999 年 12 月 31 日晚上 11:59:59 之间的日期和时间354. /**//// &summary&355. /// 是否datetime类型(表示时间上的一刻): 范围在公元(基督纪元)0001 年 1 月 1 日午夜 12:00:00 到公元 (c.e.) 9999 年 12 月 31 日晚上 11:59:59 之间的日期和时间 356. /// &/summary& 357. /// &returns&boolean&/returns& 358. protected bool isdatetime()359. {360. try361. {362. datetime.parse(_string);363. }364. catch365. {366.367. }368.369. } 370. #endregion 371.372. 是否date类型(表示时间的日期部分): 范围在公元(基督纪元)0001 年 1 月 1 日 到公元 (c.e.) 9999 年 12 月 31 日之间的日期#region 是否date类型(表示时间的日期部分): 范围在公元(基督纪元)0001 年 1 月 1 日 到公元 (c.e.) 9999 年 12 月 31 日之间的日期373. /**//// &summary&374. /// 是否date类型(表示时间的日期部分): 范围在公元(基督纪元)0001 年 1 月 1 日 到公元 (c.e.) 9999 年 12 月 31 日之间的日期 375. /// &/summary& 376. /// &returns&boolean&/returns& 377. protected bool isdate()378. {379.380. try381. {382. value = datetime.parse(_string);383. }384. catch385. {386.387. }388. if (value.date.tostring() == _string)389. {390.391. }392. else393. {394.395. }396. } 397. #endregion 398.399. 是否time类型(表示时间部分hhmmss): 范围在夜 12:00:00 到晚上 11:59:59 之间的时间#region 是否time类型(表示时间部分hhmmss): 范围在夜 12:00:00 到晚上 11:59:59 之间的时间400. /**//// &summary&401. /// 是否time类型(表示时间部分hhmmss): 范围在夜 12:00:00 到晚上 11:59:59 之间的时间 402. /// &/summary& 403. /// &returns&boolean&/returns& 404. protected bool istime()405. {406.407. try408. {409. value = datetime.parse(_string);410. }411. catch412. {413.414. }415. if (value.year == 1 && value.month == 1 && value.day == 1)416. {417.418. }419. else420. {421.422. }423. } 424. #endregion 425.426. 是否ipaddress类型(ipv4 的情况下使用以点分隔的四部分表示法格式表示,ipv6 的情况下使用冒号与十六进制格式表示)#region 是否ipaddress类型(ipv4 的情况下使用以点分隔的四部分表示法格式表示,ipv6 的情况下使用冒号与十六进制格式表示)427. /**//// &summary&428. /// 是否ipaddress类型(ipv4 的情况下使用以点分隔的四部分表示法格式表示,ipv6 的情况下使用冒号与十六进制格式表示) 429. /// &/summary& 430. /// &returns&boolean&/returns& 431. protected bool isipaddress()432. {433. try434. {435. system.net.ipaddress.parse(_string);436. }437. catch438. {439.440. }441.442. } 443. #endregion 444.445. 是否中国电话号码类型(xxx/xxxx-xxxxxxx/xxxxxxxx (/d{3,4})-?/d{7,8}):判断是否是(区号:3或4位)-(电话号码:7或8位)#region 是否中国电话号码类型(xxx/xxxx-xxxxxxx/xxxxxxxx (/d{3,4})-?/d{7,8}):判断是否是(区号:3或4位)-(电话号码:7或8位)446. /**//// &summary&447. /// 是否中国电话号码类型(xxx/xxxx-xxxxxxx/xxxxxxxx (/d{3,4})-?/d{7,8}):判断是否是(区号:3或4位)-(电话号码:7或8位) 448. /// &/summary& 449. /// &returns&boolean&/returns& 450. protected bool ischinaphone()451. {452. arraylist aryresult = new arraylist();453. return commregularmatch(_string, @(/d{3,4})-?/d{7,8}, regexoptions.none, ref aryresult, _isentirety);454. } 455. #endregion 456.457. 是否中国邮政编码(6位数字 /d{6})#region 是否中国邮政编码(6位数字 /d{6})458. /**//// &summary&459. /// 是否中国邮政编码(6位数字 /d{6}) 460. /// &/summary& 461. /// &returns&boolean&/returns& 462. protected bool ischinesepostalcode()463. {464. arraylist aryresult = new arraylist();465. return commregularmatch(_string, @/d{6}, regexoptions.none, ref aryresult, _isentirety);466. } 467. #endregion 468.469. 是否中国移动电话号码(13开头的总11位数字 13/d{9})#region 是否中国移动电话号码(13开头的总11位数字 13/d{9})470. /**//// &summary&471. /// 是否中国移动电话号码(13开头的总11位数字 13/d{9}) 472. /// &/summary& 473. /// &returns&boolean&/returns& 474. protected bool ischinesemobile()475. {476. arraylist aryresult = new arraylist();477. return commregularmatch(_string, @13/d{9}, regexoptions.none, ref aryresult, _isentirety);478. } 479. #endregion 480.481. 是否email类型(xxx@xxx.xxx /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*)#region 是否email类型(xxx@xxx.xxx /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*)482. /**//// &summary&483. /// 是否email类型(xxx@xxx.xxx /w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*) 484. /// &/summary& 485. /// &returns&boolean&/returns& 486. protected bool isemail()487. {488. arraylist aryresult = new arraylist();489. return commregularmatch(_string, @/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*, regexoptions.none, ref aryresult, _isentirety);490. } 491. #endregion 492.493. 是否internet url地址类型(http://)#region 是否internet url地址类型(http://) 494. /**//// &summary&495. /// 是否internet url地址类型(http://) 496. /// &/summary& 497. /// &returns&boolean&/returns& 498. protected bool isurl()499. {500. arraylist aryresult = new arraylist();501. return commregularmatch(_string, @http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?, regexoptions.none, ref aryresult, _isentirety);502. } 503. #endregion 504.505. 是否中文字符([/u4e00-/u9fa5])#region 是否中文字符([/u4e00-/u9fa5])506. /**//// &summary&507. /// 是否中文字符([/u4e00-/u9fa5]) 508. /// &/summary& 509. /// &returns&boolean&/returns& 510. protected bool ischineseword()511. {512. arraylist aryresult = new arraylist();513. return commregularmatch(_string, @[/u4e00-/u9fa5], regexoptions.none, ref aryresult, _isentirety);514. } 515. #endregion 516.517. 是否是数字(0到9的数字[/d]+):不包括符号.和-#region 是否是数字(0到9的数字[/d]+):不包括符号.和-518. /**//// &summary&519. /// 是否是数字(0到9的数字[/d]+):不包括符号.和- 520. /// &/summary& 521. /// &returns&boolean&/returns& 522. protected bool isnumber()523. {524. arraylist aryresult = new arraylist();525. return commregularmatch(_string, @[/d]+, regexoptions.none, ref aryresult, _isentirety);526. } 527. #endregion 528.529. 是否只包含数字,英文和下划线([/w]+)#region 是否只包含数字,英文和下划线([/w]+)530. /**//// &summary&531. /// 是否只包含数字,英文和下划线([/w]+) 532. /// &/summary& 533. /// &returns&boolean&/returns& 534. protected bool isstringmodel_01()535. {536. arraylist aryresult = new arraylist();537. return commregularmatch(_string, @[/w]+, regexoptions.none, ref aryresult, _isentirety);538. } 539. #endregion 540.541. 是否大写首字母的英文字母([a-z][a-z]+)#region 是否大写首字母的英文字母([a-z][a-z]+)542. /**//// &summary&543. /// 是否大写首字母的英文字母([a-z][a-z]+) 544. /// &/summary& 545. /// &returns&boolean&/returns& 546. protected bool isstringmodel_02()547. {548. arraylist aryresult = new arraylist();549. return commregularmatch(_string, @[a-z][a-z]+, regexoptions.none, ref aryresult, _isentirety);550. } 551. #endregion 552.553. 是否全角字符([^/x00-/xff]):包括汉字在内#region 是否全角字符([^/x00-/xff]):包括汉字在内554. /**//// &summary&555. /// 是否全角字符([^/x00-/xff]):包括汉字在内 556. /// &/summary& 557. /// &returns&boolean&/returns& 558. protected bool iswideword()559. {560. arraylist aryresult = new arraylist();561. return commregularmatch(_string, @[^/x00-/xff], regexoptions.none, ref aryresult, _isentirety);562. } 563. #endregion 564.565. 是否半角字符([/x00-/xff])#region 是否半角字符([/x00-/xff])566. /**//// &summary&567. /// 是否半角字符([^/x00-/xff]):包括汉字在内 568. /// &/summary& 569. /// &returns&boolean&/returns& 570. protected bool isnarrowword()571. {572. arraylist aryresult = new arraylist();573. return commregularmatch(_string, @[/x00-/xff], regexoptions.none, ref aryresult, _isentirety);574. } 575. #endregion 576.577. 是否合法的中国身份证号码#region 是否合法的中国身份证号码578. protected bool ischineseid()579. {580. if (_string.length == 15)581. {582. _string = cidupdate(_string);583. }584. if (_string.length == 18)585. {586. string strresult = checkcidinfo(_string);587. if (strresult == 非法地区 || strresult == 非法生日 || strresult == 非法证号)588. {589.590. }591. else592. {593.594. }595. }596. else597. {598.599. }600. } 601. #endregion 602.603. #endregion 604.605. 通用正则表达式判断函数#region 通用正则表达式判断函数606. /**//// &summary&607. /// 通用正则表达式判断函数 608. /// &/summary& 609. /// &param name=strverifystring&string,用于匹配的字符串&/param& 610. /// &param name=strregular&string,正则表达式&/param& 611. /// &param name=regoption&regexoptions,配置正则表达式的选项&/param& 612. /// &param name=aryresult&arraylist,分解的字符串内容&/param& 613. /// &param name=isentirety&boolean,是否需要完全匹配&/param& 614. /// &returns&&/returns& 615. public bool commregularmatch(string strverifystring, string strregular, system.text.regularexpressions.regexoptions regoption, ref system.collections.arraylist aryresult, bool isentirety)616. {617. system.text.regularexpressions.618. system.text.regularexpressions.619.620. 如果需要完全匹配的处理#region 如果需要完全匹配的处理621. if (isentirety)622. {623. strregular = strregular.insert(0, @/a);624. strregular = strregular.insert(strregular.length, @/z);625. } 626. #endregion 627.628. try629. {630. r = new system.text.regularexpressions.regex(strregular, regoption);631. }632. catch (system.exception e)633. {634. throw (e);635. }636.637. for (m = r.match(strverifystring); m. m = m.nextmatch())638. {639. aryresult.add(m);640. }641.642. if (aryresult.count == 0)643. {644.645. }646. else647. {648.649. }650. } 651. #endregion 652.653. 中国身份证号码验证#region 中国身份证号码验证654. private string checkcidinfo(string cid)655. {656. string[] acity = new string[] { null, null, null, null, null, null, null, null, null, null, null, 北京, 天津, 河北, 山西, 内蒙古, null, null, null, null, null, 辽宁, 吉林, 黑龙江, null, null, null, null, null, null, null, 上海, 江苏, 浙江, 安微, 福建, 江西, 山东, null, null, null, 河南, 湖北, 湖南, 广东, 广西, 海南, null, null, null, 重庆, 四川, 贵州, 云南, 西藏, null, null, null, null, null, null, 陕西, 甘肃, 青海, 宁夏, 新疆, null, null, null, null, null, 台湾, null, null, null, null, null, null, null, null, null, 香港, 澳门, null, null, null, null, null, null, null, null, 国外 };657. double isum = 0;658. string info = string.659. system.text.regularexpressions.regex rg = new system.text.regularexpressions.regex(@^/d{17}(/d|x)$);660. system.text.regularexpressions.match mc = rg.match(cid);661. if (!mc.success)662. {663. return string.664. }665. cid = cid.tolower();666. cid = cid.replace(x, a);667. if (acity[int.parse(cid.substring(0, 2))] == null)668. {669. return 非法地区;670. }671. try672. {673. datetime.parse(cid.substring(6, 4) +
+ cid.substring(10, 2) +
+ cid.substring(12, 2));674. }675. catch676. {677. return 非法生日;678. }679. for (int i = 17; i &= 0; i--)680. {681. isum += (system.math.pow(2, i) % 11) * int.parse(cid[17 - i].tostring(), system.globalization.numberstyles.hexnumber);682. }683. if (isum % 11 != 1)684. {685. return (非法证号);686. }687. else688. {689. return (acity[int.parse(cid.substring(0, 2))] + , + cid.substring(6, 4) + - + cid.substring(10, 2) + - + cid.substring(12, 2) + , + (int.parse(cid.substring(16, 1)) % 2 == 1 ? 男 : 女));690. }691. } 692. #endregion 693.694. 身份证号码15升级为18位#region 身份证号码15升级为18位695. private string cidupdate(string shortcid)696. {697. char[] strjiaoyan = { '1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2' };698. int[] intquan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };699.700. int inttemp = 0;701.702. strtemp = shortcid.substring(0, 6) + 19 + shortcid.substring(6);703. for (int i = 0; i &= strtemp.length - 1; i++)704. {705. inttemp += int.parse(strtemp.substring(i, 1)) * intquan[i];706. }707. inttemp = inttemp % 11;708. return strtemp + strjiaoyan[inttemp];709. } 710. #endregion 711. }712.}更多数据库信息请查看:

我要回帖

更多关于 大于100的正则表达式 的文章

 

随机推荐