1)angularjs中如何使用正则表达式
<input type="text" ng-model="dutyCode" ng-focus="showDutyCodeTips = true"
ng-blur="showDutyCodeTips = false" ng-pattern="/^CV[0-9A-Z]{5}$/" >
<span ng-init="showDutyCodeTips = false" ng-show="showDutyCodeTips"
style="color:red"> 责任编码规则为:CV+5位(数字或大写字母) </span>
2)js中如何使用正则表达式
.)使用RegExp能动态的设置正则表达式
if(! new RegExp("PL" + param.productClass + "[0-9A-Z]{5}").test(param.planCode)){
uiTips.alert("险种编码规则:PL+2位产品大类编码+5位(数字或大写字母)");
return;
}
.) 这样直接使用正则表达式(表达式不能拼凑)
var reg =/^PL03[0-9A-Z]{5}$/;
console.log(reg.test("PL03AAAAA"));
3)java代码中使用正则表达式
String postfix = "abcde"
return Pattern.compile("[0-9A-Z]{5}").matcher(postfix).matches();
public static void charReplace() {
String regex = "a+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("okaaaa LetmeAseeaaa aa booa");
String s = matcher.replaceAll("A");
System.out.println(s);
}
public static void getDate(String str) {
String regEx = "([a-zA-Z]+)|//s+[0-9]{1,2},//s*[0-9]{4}";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(str);
if (!matcher.find()) {
System.out.println("日期格式错误!");
return;
}
System.out.println(matcher.group(1));
//分组的索引值是从1开始的,所以取第一个分组的方法是m.group(1)而不是m.group(0)。
}
public static void getString(String str) {
String regex = ".+/(.+)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if (!matcher.find()) {
System.out.println("文件路径格式不正确!");
return;
}
System.out.println(matcher.group(1));
}
public static void getChinese(String str) {
String regex = "[\u4E00-\u9FFF]+";//[//u4E00-//u9FFF]为汉字
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String termp = matcher.group();
System.out.println(termp);
sb.append(termp);
}
System.out.println(sb);
}
public static void validateEmail(String email) {
String regex = "[0-9a-zA-Z]+@[0-9a-zA-Z]+//.[0-9a-zA-Z]+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
if (matcher.matches()) {
System.out.println("这是合法的Email");
} else {
System.out.println("这是非法的Email");
}
}
public static void main(String[] args) {
//1)验证字符串的格式是否正确
Pattern pattern = Pattern.compile("b*g");
Matcher matcher = pattern.matcher("bbg");
//System.out.println(matcher.matches());
//System.out.println(pattern.matches("b*g", "bbg"));
//验证邮政编码
//System.out.println(pattern.matches("[0-9]{6}", "200038"));
//System.out.println(pattern.matches("\\d{6}", "200038"));
//验证电话号码
System.out.println(pattern.matches("[0-9]{3,4}-?[0-9]+", "02178989799"));
getDate("Nov 10,2009");
//验证***:判断一个字符串是不是***号码,即是否是15或18位数字。
System.out.println(pattern.matches("^\\d{15}|\\d{18}$", "123456789009876"));
//2)查询字符串中满足条件的子串,并将其替换掉
charReplace();
//3)提前匹配到的子串
getChinese("welcome to china,江西奉新,welcome,你!");
validateEmail("luosijin123@163.com");
}
转载于:https://blog.51cto.com/6817977/1694660