好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java常用正则表达式验证类完整实例【邮箱、URL、IP、电话、身份证等】

本文实例讲述了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

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

package com.fsti.icop.util.regexp;

import java.util.regex.matcher;

import java.util.regex.pattern;

public final class regexpvalidatorutils {

/**

* 验证邮箱

*

* @param 待验证的字符串

* @return 如果是符合的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isemail(string str) {

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})(\\]?)$" ;

return match(regex, str);

}

/**

* 验证ip地址

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isip(string str) {

string num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)" ;

string regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$" ;

return match(regex, str);

}

/**

* 验证网址url

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isurl(string str) {

string regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?" ;

return match(regex, str);

}

/**

* 验证电话号码

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean istelephone(string str) {

string regex = "^(\\d{3,4}-)?\\d{6,8}$" ;

return match(regex, str);

}

/**

* 验证输入密码条件(字符与数据同时出现)

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean ispassword(string str) {

string regex = "[a-za-z]+[0-9]" ;

return match(regex, str);

}

/**

* 验证输入密码长度 (6-18位)

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean ispasswlength(string str) {

string regex = "^\\d{6,18}$" ;

return match(regex, str);

}

/**

* 验证输入邮政编号

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean ispostalcode(string str) {

string regex = "^\\d{6}$" ;

return match(regex, str);

}

/**

* 验证输入手机号码

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean ishandset(string str) {

string regex = "^[1]+[3,5]+\\d{9}$" ;

return match(regex, str);

}

/**

* 验证输入身份证号

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isidcard(string str) {

string regex = "(^\\d{18}$)|(^\\d{15}$)" ;

return match(regex, str);

}

/**

* 验证输入两位小数

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isdecimal(string str) {

string regex = "^[0-9]+(.[0-9]{2})?$" ;

return match(regex, str);

}

/**

* 验证输入一年的12个月

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean ismonth(string str) {

string regex = "^(0?[[1-9]|1[0-2])$" ;

return match(regex, str);

}

/**

* 验证输入一个月的31天

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isday(string str) {

string regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$" ;

return match(regex, str);

}

/**

* 验证日期时间

*

* @param 待验证的字符串

* @return 如果是符合网址格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isdate(string str) {

// 严格验证时间格式的(匹配[2002-01-31], [1997-04-30],

// [2004-01-01])不匹配([2002-01-32], [2003-02-29], [04-01-01])

// string regex =

// "^((((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)))))$";

// 没加时间验证的yyyy-mm-dd

// 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-))$";

// 加了时间验证的yyyy-mm-dd 0

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$" ;

return match(regex, str);

}

/**

* 验证数字输入

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isnumber(string str) {

string regex = "^[0-9]*$" ;

return match(regex, str);

}

/**

* 验证非零的正整数

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isintnumber(string str) {

string regex = "^\\+?[1-9][0-9]*$" ;

return match(regex, str);

}

/**

* 验证大写字母

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isupchar(string str) {

string regex = "^[a-z]+$" ;

return match(regex, str);

}

/**

* 验证小写字母

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean islowchar(string str) {

string regex = "^[a-z]+$" ;

return match(regex, str);

}

/**

* 验证验证输入字母

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean isletter(string str) {

string regex = "^[a-za-z]+$" ;

return match(regex, str);

}

/**

* 验证验证输入汉字

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean ischinese(string str) {

string regex = "^[\u4e00-\u9fa5],{0,}$" ;

return match(regex, str);

}

/**

* 验证验证输入字符串

*

* @param 待验证的字符串

* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>

*/

public static boolean islength(string str) {

string regex = "^.{8,}$" ;

return match(regex, str);

}

/**

* @param regex

* 正则表达式字符串

* @param str

* 要匹配的字符串

* @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;

*/

private static boolean match(string regex, string str) {

pattern pattern = pattern测试数据pile(regex);

matcher matcher = pattern.matcher(str);

return matcher.matches();

}

// 3. 检查字符串重复出现的词

//

// private void btnword_click(object sender, eventargs e)

// {

// system.text.regularexpressions.matchcollection matches =

// system.text.regularexpressions.regex.matches(label1.text,

//

// @"\b(?<word>\w+)\s+(\k<word>)\b",

// system.text.regularexpressions.regexoptions测试数据piled |

// system.text.regularexpressions.regexoptions.ignorecase);

// if (matches.count != 0)

// {

// foreach (system.text.regularexpressions.match match in matches)

// {

// string word = match.groups["word"].value;

// messagebox.show(word.tostring(),"英文单词");

// }

// }

// else { messagebox.show("没有重复的单词"); }

//

//

// }

//

// 4. 替换字符串

//

// private void button1_click(object sender, eventargs e)

// {

//

// string strresult =

// system.text.regularexpressions.regex.replace(textbox1.text,

// @"[a-za-z]\*?", textbox2.text);

// messagebox.show("替换前字符:" + "\n" + textbox1.text + "\n" + "替换的字符:" + "\n"

// + textbox2.text + "\n" +

//

// "替换后的字符:" + "\n" + strresult,"替换");

//

// }

//

// 5. 拆分字符串

//

// private void button1_click(object sender, eventargs e)

// {

// //实例: 甲025-8343243乙0755-2228382丙029-32983298389289328932893289丁

// foreach (string s in

// system.text.regularexpressions.regex.split(textbox1.text,@"\d{3,4}-\d*"))

// {

// textbox2.text+=s; //依次输出 "甲乙丙丁"

// }

//

// }

}

ps:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

正则表达式在线测试工具: https://tool.tuohang.net/t/regex/

正则表达式在线生成工具: https://tool.tuohang.net/t/regcode/

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/hacker_Lees/article/details/80280418

查看更多关于Java常用正则表达式验证类完整实例【邮箱、URL、IP、电话、身份证等】的详细内容...

  阅读:12次