日期:
public static void main(String args[])
{
String str = "1983-07-27"; // 指定好一个日期格式的字符串
String pat = "\\d{4}-\\d{2}-\\d{2}"; // 指定好正则表达式
Pattern p = Pattern.compile(pat); // 实例化Pattern类
Matcher m = p.matcher(str); // 实例化Matcher类
if (m.matches())
{ // 进行验证的匹配,使用正则
System.out.println("日期格式合法!");
}
else
{
System.out.println("日期格式不合法!");
}
}
例子2:
// 单词边界 \b
private static final String REGEX = "\\bdog\\b";
private static final String INPUT = "dog dog dog doggie dogg";
public static void main(String[] args)
{
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(INPUT); // 获得一个匹配器对象
int count = 0;
while (m.find())
{
count++;
System.out.println("Match number " + count);
System.out.println("start(): " + m.start());
System.out.println("end(): " + m.end());
}
}
// Match number 1
// start(): 0
// end(): 3
// Match number 2
// start(): 4
// end(): 7
// Match number 3
// start(): 8
// end(): 11
例子3:取出字符,也就是按照数字拆分
public static void main(String args[]){
// 要求将里面的字符取出,也就是说按照数字拆分
String str = "A1B22C333D4444E55555F" ; // 指定好一个字符串
String pat = "\\d+" ; // 指定好正则表达式
Pattern p = Pattern.compile(pat) ; // 实例化Pattern类
Matcher m = p.matcher(str) ; // 实例化Matcher类的对象
String newString = m.replaceAll("_") ;
System.out.println(newString) ;
}
//A_B_C_D_E_F
例子4:
public static void main(String args[])
{
String str1 = "A1B22C333D4444E55555F".replaceAll("\\d+", "_");
boolean temp = "1983-07-27".matches("\\d{4}-\\d{2}-\\d{2}");
String s[] = "A1B22C333D4444E55555F".split("\\d+");
System.out.println("字符串替换操作:" + str1);
System.out.println("字符串验证:" + temp);
System.out.print("字符串的拆分:");
for (int x = 0; x < s.length; x++)
{
System.out.print(s[x] + "\t");
}
}
// 字符串替换操作:A_B_C_D_E_F
// 字符串验证:true
// 字符串的拆分:A B C D E F
例子5:
public static void main(String args[])
{
String info = "LXH:98|MLDN:90|LI:100"; // 定义一个字符串
// 拆分的形式:
/*
* LXH --> 98
* MLDN --> 90
* LI --> 100
*/
String s[] = info.split("\\|");
System.out.println("s[]:"+s[0]);
System.out.println("字符串的拆分:");
for (int x = 0; x < s.length; x++)
{
String s2[] = s[x].split(":");
System.out.println(s2[0] + "\t" + s2[1]);
}
}
// s[]:LXH:98
// 字符串的拆分:
// LXH 98
// MLDN 90
// LI 100
匹配号码:
public boolean startCheck(String reg, String string)
{
boolean result = false;
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(string);
result = matcher.matches();
return result;
}
/**
* 手机号码验证,11位,不知道详细的手机号码段,只是验证开头必须是1和位数
* */
public boolean checkCellPhone(String cellPhoneNr)
{
//String reg = "^[1][\\d]{10}";
String reg = "^(13|14|15|16|17|18|19|)\\d{9}$"; //以13、15、18开头
return startCheck(reg, cellPhoneNr);
}
/**
* 格式:010-67676767,区号长度3-4位,必须以"0"开头,号码是7-8位
* */
public boolean checkPhoneNr(String phoneNr)
{
String regex = "^[0]\\d{2,3}\\-\\d{7,8}";
return startCheck(regex, phoneNr);
}
/**
* 格式:6767676, 号码位数必须是7-8位,头一位不能是"0"
* */
public boolean checkPhoneNrWithoutCode(String phoneNr)
{
String reg = "^[1-9]\\d{6,7}";
return startCheck(reg, phoneNr);
}
/**
* 格式:0106767676,共11位或者12位,必须是0开头
* */
public boolean checkPhoneNrWithoutLine(String phoneNr)
{
String reg = "^[0]\\d{10,11}$";
return startCheck(reg, phoneNr);
}