Java:正则表达式

  1. package com.fsti.icop.util.regexp;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public final class RegExpValidatorUtils {
  7. /**
  8. * 验证邮箱
  9. *
  10. * @param 待验证的字符串
  11. * @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b>
  12. */
  13. public static boolean isEmail(String str) {
  14. String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
  15. return match(regex, str);
  16. }
  17.  
  18. /**
  19. * 验证IP地址
  20. *
  21. * @param 待验证的字符串
  22. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  23. */
  24. public static boolean isIP(String str) {
  25. String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
  26. String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
  27. return match(regex, str);
  28. }
  29.  
  30. /**
  31. * 验证网址Url
  32. *
  33. * @param 待验证的字符串
  34. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  35. */
  36. public static boolean IsUrl(String str) {
  37. String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
  38. return match(regex, str);
  39. }
  40.  
  41. /**
  42. * 验证电话号码
  43. *
  44. * @param 待验证的字符串
  45. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  46. */
  47. public static boolean IsTelephone(String str) {
  48. String regex = "^(\\d{3,4}-)?\\d{6,8}$";
  49. return match(regex, str);
  50. }
  51.  
  52. /**
  53. * 验证输入密码条件(字符与数据同时出现)
  54. *
  55. * @param 待验证的字符串
  56. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  57. */
  58. public static boolean IsPassword(String str) {
  59. String regex = "[A-Za-z]+[0-9]";
  60. return match(regex, str);
  61. }
  62.  
  63. /**
  64. * 验证输入密码长度 (6-18位)
  65. *
  66. * @param 待验证的字符串
  67. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  68. */
  69. public static boolean IsPasswLength(String str) {
  70. String regex = "^\\d{6,18}$";
  71. return match(regex, str);
  72. }
  73.  
  74. /**
  75. * 验证输入邮政编号
  76. *
  77. * @param 待验证的字符串
  78. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  79. */
  80. public static boolean IsPostalcode(String str) {
  81. String regex = "^\\d{6}$";
  82. return match(regex, str);
  83. }
  84.  
  85. /**
  86. * 验证输入手机号码
  87. *
  88. * @param 待验证的字符串
  89. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  90. */
  91. public static boolean IsHandset(String str) {
  92. String regex = "^[1]+[3,5]+\\d{9}$";
  93. return match(regex, str);
  94. }
  95.  
  96. /**
  97. * 验证输入身份证号
  98. *
  99. * @param 待验证的字符串
  100. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  101. */
  102. public static boolean IsIDcard(String str) {
  103. String regex = "(^\\d{18}$)|(^\\d{15}$)";
  104. return match(regex, str);
  105. }
  106.  
  107. /**
  108. * 验证输入两位小数
  109. *
  110. * @param 待验证的字符串
  111. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  112. */
  113. public static boolean IsDecimal(String str) {
  114. String regex = "^[0-9]+(.[0-9]{2})?$";
  115. return match(regex, str);
  116. }
  117.  
  118. /**
  119. * 验证输入一年的12个月
  120. *
  121. * @param 待验证的字符串
  122. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  123. */
  124. public static boolean IsMonth(String str) {
  125. String regex = "^(0?[[1-9]|1[0-2])$";
  126. return match(regex, str);
  127. }
  128.  
  129. /**
  130. * 验证输入一个月的31天
  131. *
  132. * @param 待验证的字符串
  133. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  134. */
  135. public static boolean IsDay(String str) {
  136. String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
  137. return match(regex, str);
  138. }
  139.  
  140. /**
  141. * 验证日期时间
  142. *
  143. * @param 待验证的字符串
  144. * @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  145. */
  146. public static boolean isDate(String str) {
  147. // 严格验证时间格式的(匹配[2002-01-31], [1997-04-30],
  148. // [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])
  149. // String regex =
  150. // "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$";
  151. // 没加时间验证的YYYY-MM-DD
  152. // String regex =
  153. // "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
  154. // 加了时间验证的YYYY-MM-DD 00:00:00
  155. String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$";
  156. return match(regex, str);
  157. }
  158.  
  159. /**
  160. * 验证数字输入
  161. *
  162. * @param 待验证的字符串
  163. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  164. */
  165. public static boolean IsNumber(String str) {
  166. String regex = "^[0-9]*$";
  167. return match(regex, str);
  168. }
  169.  
  170. /**
  171. * 验证非零的正整数
  172. *
  173. * @param 待验证的字符串
  174. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  175. */
  176. public static boolean IsIntNumber(String str) {
  177. String regex = "^\\+?[1-9][0-9]*$";
  178. return match(regex, str);
  179. }
  180.  
  181. /**
  182. * 验证大写字母
  183. *
  184. * @param 待验证的字符串
  185. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  186. */
  187. public static boolean IsUpChar(String str) {
  188. String regex = "^[A-Z]+$";
  189. return match(regex, str);
  190. }
  191.  
  192. /**
  193. * 验证小写字母
  194. *
  195. * @param 待验证的字符串
  196. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  197. */
  198. public static boolean IsLowChar(String str) {
  199. String regex = "^[a-z]+$";
  200. return match(regex, str);
  201. }
  202.  
  203. /**
  204. * 验证验证输入字母
  205. *
  206. * @param 待验证的字符串
  207. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  208. */
  209. public static boolean IsLetter(String str) {
  210. String regex = "^[A-Za-z]+$";
  211. return match(regex, str);
  212. }
  213.  
  214. /**
  215. * 验证验证输入汉字
  216. *
  217. * @param 待验证的字符串
  218. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  219. */
  220. public static boolean IsChinese(String str) {
  221. String regex = "^[\u4e00-\u9fa5],{0,}$";
  222. return match(regex, str);
  223. }
  224.  
  225. /**
  226. * 验证验证输入字符串
  227. *
  228. * @param 待验证的字符串
  229. * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
  230. */
  231. public static boolean IsLength(String str) {
  232. String regex = "^.{8,}$";
  233. return match(regex, str);
  234. }
  235.  
  236. /**
  237. * @param regex
  238. * 正则表达式字符串
  239. * @param str
  240. * 要匹配的字符串
  241. * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
  242. */
  243. private static boolean match(String regex, String str) {
  244. Pattern pattern = Pattern.compile(regex);
  245. Matcher matcher = pattern.matcher(str);
  246. return matcher.matches();
  247. }
  248.  
  249. // 3. 检查字符串重复出现的词
  250. //
  251. // private void btnWord_Click(object sender, EventArgs e)
  252. // {
  253. // System.Text.RegularExpressions.MatchCollection matches =
  254. // System.Text.RegularExpressions.Regex.Matches(label1.Text,
  255. //
  256. // @"\b(?<word>\w+)\s+(\k<word>)\b",
  257. // System.Text.RegularExpressions.RegexOptions.Compiled |
  258. // System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  259. // if (matches.Count != 0)
  260. // {
  261. // foreach (System.Text.RegularExpressions.Match match in matches)
  262. // {
  263. // string word = match.Groups["word"].Value;
  264. // MessageBox.Show(word.ToString(),"英文单词");
  265. // }
  266. // }
  267. // else { MessageBox.Show("没有重复的单词"); }
  268. //
  269. //
  270. // }
  271. //
  272. // 4. 替换字符串
  273. //
  274. // private void button1_Click(object sender, EventArgs e)
  275. // {
  276. //
  277. // string strResult =
  278. // System.Text.RegularExpressions.Regex.Replace(textBox1.Text,
  279. // @"[A-Za-z]\*?", textBox2.Text);
  280. // MessageBox.Show("替换前字符:" + "\n" + textBox1.Text + "\n" + "替换的字符:" + "\n"
  281. // + textBox2.Text + "\n" +
  282. //
  283. // "替换后的字符:" + "\n" + strResult,"替换");
  284. //
  285. // }
  286. //
  287. // 5. 拆分字符串
  288. //
  289. // private void button1_Click(object sender, EventArgs e)
  290. // {
  291. // //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁
  292. // foreach (string s in
  293. // System.Text.RegularExpressions.Regex.Split(textBox1.Text,@"\d{3,4}-\d*"))
  294. // {
  295. // textBox2.Text+=s; //依次输出 "甲乙丙丁"
  296. // }
  297. //
  298. // }
  299.  
  300. }

常用正则表达式:

  1. 常用正则表达式
  2. 匹配特定数字:
  3. ^[1-9]\d*$    //匹配正整数
  4. ^-[1-9]\d*$   //匹配负整数
  5. ^-?[1-9]\d*$   //匹配整数
  6. ^[1-9]\d*|0$  //匹配非负整数(正整数 + 0)
  7. ^-[1-9]\d*|0$   //匹配非正整数(负整数 + 0)
  8. ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$   //匹配正浮点数
  9. ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$  //匹配负浮点数
  10. ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$  //匹配浮点数
  11. ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$   //匹配非负浮点数(正浮点数 + 0)
  12. (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$  //匹配非正浮点数(负浮点数 + 0)
  13. 评注:处理大量数据时有用,具体应用时注意修正
  14.  
  15. 匹配特定字符串:
  16. ^[A-Za-z]+$  //匹配由26个英文字母组成的字符串
  17. ^[A-Z]+$  //匹配由26个英文字母的大写组成的字符串
  18. ^[a-z]+$  //匹配由26个英文字母的小写组成的字符串
  19. ^[A-Za-z0-9]+$  //匹配由数字和26个英文字母组成的字符串
  20. ^\w+$  //匹配由数字、26个英文字母或者下划线组成的字符串
  21.  
  22. 用户名:/^[a-z0-9_-]{3,16}$/
  23. 密码:/^[a-z0-9_-]{6,18}$/
  24. 十六进制值:/^#?([a-f0-9]{6}|[a-f0-9]{3})$/
  25. 电子邮箱:/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
  26. URL:/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
  27. IP 地址:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
  28. HTML 标签:/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
  29. Unicode编码中的汉字范围:/^[u4e00-u9fa5],{0,}$/
  30. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]
  31. 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
  32.  
  33. 匹配双字节字符(包括汉字在内):[^\x00-\xff]
  34. 评注:可以用来计算字符串的长度(一个双字节字符长度计2ASCII字符计1
  35. 匹配空白行的正则表达式:\n\s*\r
  36. 评注:可以用来删除空白行
  37. 匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?</\1>|<.*? />
  38. 评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
  39. 匹配首尾空白字符的正则表达式:^\s*|\s*$
  40. 评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
  41. 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
  42. 评注:表单验证时很实用
  43. 匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
  44. 评注:网上流传的版本功能很有限,上面这个基本可以满足需求
  45. 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
  46. 评注:表单验证时很实用
  47. 匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
  48. 评注:匹配形式如 0511-4405222 021-87888822
  49. 匹配腾讯QQ号:[1-9][0-9]{4,}
  50. 评注:腾讯QQ号从10000开始
  51. 匹配中国大陆邮政编码:[1-9]\d{5}(?!\d)
  52. 评注:中国大陆邮政编码为6位数字
  53. 匹配ip地址:\d+\.\d+\.\d+\.\d+
  54. 评注:提取ip地址时有用
  55. 网址(URL [a-zA-z]+://[^\s]*
  56. IP地址(IP Address) ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)
  57. 电子邮件(Email) \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
  58. QQ号码 [1-9]\d{4,}
  59. HTML标记(包含内容或自闭合) <(.*)(.*)>.*<\/\1>|<(.*) \/>
  60. 密码(由数字/大写字母/小写字母/标点符号组成,四种都必有,8位以上) (?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$
  61. 日期(年-月-日) (\d{4}|\d{2})-((0?([1-9]))|(1[1|2]))-((0?[1-9])|([12]([1-9]))|(3[0|1]))
  62. 日期(月/日/年) ((0?[1-9]{1})|(1[1|2]))/(0?[1-9]|([12][1-9])|(3[0|1]))/(\d{4}|\d{2})
  63. 时间(小时:分钟, 24小时制) ((1|0?)[0-9]|2[0-3]):([0-5][0-9])
  64. 汉字(字符) [\u4e00-\u9fa5]
  65. 中文及全角标点符号(字符) [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee]
  66. 中国大陆固定电话号码 (\d{4}-|\d{3}-)?(\d{8}|\d{7})
  67. 中国大陆手机号码 1\d{10}
  68. 中国大陆邮政编码 [1-9]\d{5}
  69. 中国大陆身份证号(15位或18位) \d{15}(\d\d[0-9xX])?
  70. 非负整数(正整数或零) \d+
  71. 正整数 [0-9]*[1-9][0-9]*
  72. 负整数 -[0-9]*[1-9][0-9]*
  73. 整数 -?\d+
  74. 小数 (-?\d+)(\.\d+)?

Java:正则表达式的更多相关文章

  1. java正则表达式

    java正则表达式 1.Java正则表达式的语法与示例:  http://baike.xsoftlab.net/view/207.html 2.Java 正则表达式:  http://www.runo ...

  2. Java正则表达式入门——转自RUNOOB.COM

    Java 正则表达式 正则表达式定义了字符串的模式. 正则表达式可以用来搜索.编辑或处理文本. 正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. Java正则表达式和Perl的是最为相似 ...

  3. Java 正则表达式详解

    Java 提供了功能强大的正则表达式API,在java.util.regex 包下.本教程介绍如何使用正则表达式API. 正则表达式 一个正则表达式是一个用于文本搜索的文本模式.换句话说,在文本中搜索 ...

  4. 【转】详解Java正则表达式语法

    (转自: http://www.jb51.net/article/76354.htm) 这篇文章主要介绍了Java正则表达式语法,包括常用正则表达式.匹配验证-验证Email是否正确以及字符串中查询字 ...

  5. java正则表达式【大全】

    [正则表达式]文本框输入内容控制整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$& ...

  6. JAVA正则表达式:Pattern类与Matcher类详解(转)

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...

  7. JAVA正则表达式:Pattern类与Matcher类详解

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...

  8. Java 正则表达式[转载]

    PS:转载自CSDN博客看上去很美 众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学 ...

  9. Java正则表达式的应用

    在很多种情况下,我们都必须对字符串进行匹配,以便判断字符串的格式是否符合要求,对字符串中的内容进行提取.比如,我要从一段话aabdfe中,判断这段话是否有包含ab这个词,那么如果用if-else来判断 ...

  10. Java正则表达式实用教程

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.java.util.regex包主要包括以下三个类:Pattern.Matcher和PatternSynta ...

随机推荐

  1. cinder服务端的keystone认证机制

    keystone在openstack中的地位 Keystone作为OpenStack中的身份管理与授权模块,主要实现系统用户的身份认证.基于角色的授权管理.其他OpenStack服务的地址发现和安全策 ...

  2. 设置grid高度

    $("#jqxSalaryCalculation").jqxGrid({ height: $("#jqxTree").height() - 73 });

  3. VSCode调试配置

    http://code.visualstudio.com/docs/editor/debugging#_launch-configurations VSCode内置Node.js运行时, 能调试jav ...

  4. 【BZOJ1570】[JSOI2008]Blue Mary的旅行 动态加边网络流

    [BZOJ1570][JSOI2008]Blue Mary的旅行 Description 在一段时间之后,网络公司终于有了一定的知名度,也开始收到一些订单,其中最大的一宗来自B市.Blue Mary决 ...

  5. 170117、spring解决乱码

    spring解决乱码这个问题网上有很多解决方法,也可以关注本博客的文章,在此不再赘述, 今天推荐大家另外两种解决方法! 问题现象: 1.后台代码 2.前端界面 解决方法: 方法1:是在后台请求方法上加 ...

  6. Android Download机制详解(一)DocumentUI部分

    在Android中Google为我们集成了一套十分便利的Download机制,用来下载网络上的资源文件.以此省去了我们编写和维护大量与Download相关的代码. 组成 Android中Downloa ...

  7. YOLO v1论文笔记

    You Only Look Once:Unified, Real-Time Object Detection   论文链接:https://arxiv.org/abs/1506.02640 Homep ...

  8. Linux挂载:VMware tools for Linux安装

    安装VMTools [root@localhost ~]# mkdir /mnt/cdrom [root@localhost ~]# mount /dev/cdrom /mnt/cdrom/ [roo ...

  9. C++ 动态内存 栈堆

    C++ 动态内存_w3cschool https://www.w3cschool.cn/cpp/cpp-dynamic-memory.html

  10. Java 语言基础之数组(一)

    数组定义及格式: 数组: 同一种类型数据的集合, 就是一个容器 定义数组格式1: 元素类型[] 数组名 = new 元素类型[元素个数(即数组长度)]; 说明: 数组是一个容器.而容器属于一个实体,实 ...