|
使用java的正则匹配之前要记得导包
import java.util.regex.Matcher;
import java.util.regex.Pattern;
1.使用正则判断是否有数字、字母 public static void main(String[] args) {
System.out.println(containsNum("111"));
System.out.println(containsLetter("aaa"));
System.out.println(containsNumOrLetter("aaa"));
}
//判断是否含有字母
public static boolean containsLetter(String str) {
String patt="";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
}
//判断是否含有数字
public static boolean containsNum(String str) {
String patt="[0-9]";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
}
//判断是否含有数字或字母
public static boolean containsNumOrLetter(String str) {
String patt="[0-9|a-z|A-Z]";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
} |
2.那如果是判断是不含有字母或数字的正则怎么写呢?
public static void main(String[] args) {
System.out.println(containsNoNum("111"));
System.out.println(containsNoLetter("aaa"));
System.out.println(containsNoNumOrLetter("aaa"));
}
public static boolean containsNoLetter(String str) {
String patt="[^a-z]";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
}
public static boolean containsNoNum(String str) {
String patt="[^0-9]";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
}
public static boolean containsNoNumOrLetter(String str) {
String patt="[^0-9|a-z|A-Z]";
Pattern r = Pattern.compile(patt);
Matcher matcher = r.matcher(str);
return matcher.find();
} |
3.根据上面的正则来写一个比较版本号的方法
public static void main(String[] args) {
System.out.println(compareVersion("1.3_bc", "1.3_b"));
}
public static String compareVersion(String version1, String version2) {
//忽略大小写,只保留数字和字母和点
String v1 = version1.toLowerCase().replaceAll("[^0-9|a-z|.]", "");
String v2 = version2.toLowerCase().replaceAll("[^0-9|a-z|.]", "");
//按点来切割
String[] arr1 = v1.split("\\.");
String[] arr2 = v2.split("\\.");
int len = arr1.length > arr2.length ? arr2.length : arr1.length;
//正则判断是否含有字母
String patt = "";
Pattern r = Pattern.compile(patt);
int i = 0;
do {
Matcher m1 = r.matcher(arr1[i]);
Matcher m2 = r.matcher(arr2[i]);
boolean flag = false;
//判断是否含有字母
if (m1.find() && m2.find()) {//如果都含有字母
//先比较数字
String num1 = arr1[i].substring(0, m1.start());
String num2 = arr2[i].substring(0, m2.start());
if (Integer.parseInt(num1) > Integer.parseInt(num2)) {
return version1;
} else if (Integer.parseInt(num1) < Integer.parseInt(num2)) {
return version2;
}
//再比较含有字母的字符串
String str1 = arr1[i].substring(m1.start());
String str2 = arr2[i].substring(m2.start());
if (str1.compareTo(str2) > 0)
return version1;
else if (str1.compareTo(str2) < 0)
return version2;
} else if ((flag = m1.find()) || m2.find()) {//如果只是其中一个含有字母
String vContains = flag ? arr1[i] : arr2[i];
String vOther = flag ? arr2[i] : arr1[i];
//先比较数字
String num1 = vContains.substring(0, flag ? m1.start() : m2.start());
String num2 = vOther;
if (Integer.parseInt(num1) > Integer.parseInt(num2)) {
return version1;
} else if (Integer.parseInt(num1) < Integer.parseInt(num2)) {
return version2;
}
return flag ? version1 : version2;
} else {//如果都不含有字母,则直接比较数字
if (Integer.parseInt(arr1[i]) > Integer.parseInt(arr2[i])) {
return version1;
} else if (Integer.parseInt(arr1[i]) < Integer.parseInt(arr2[i])) {
return version2;
}
}
} while (++i < len);
//以上判断都没有结果则返回数组 长度较长的那个版本
return arr1.length > arr2.length ? version1 : version2;
} |
————————————————
原文链接:https://blog.csdn.net/a_hui_tai_lang/article/details/81350422
程序猿的技术大观园:www.javathinker.net
|
网站系统异常
系统异常信息 |
Request URL:
http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=1235
java.lang.NullPointerException
如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员。
|
|