正则表达式在js和java中的使用
1)angularjs中如何使用正則表達(dá)式
<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"> 責(zé)任編碼規(guī)則為:CV+5位(數(shù)字或大寫字母) </span>
2)js中如何使用正則表達(dá)式
? ?.)使用RegExp能動(dòng)態(tài)的設(shè)置正則表達(dá)式
? ? if(! new RegExp("PL" + param.productClass + "[0-9A-Z]{5}").test(param.planCode)){
uiTips.alert("險(xiǎn)種編碼規(guī)則:PL+2位產(chǎn)品大類編碼+5位(數(shù)字或大寫字母)");?
return;
? ? }
? ?.) 這樣直接使用正則表達(dá)式(表達(dá)式不能拼湊)
? ? ? ? var reg =/^PL03[0-9A-Z]{5}$/;
? ? ? ? console.log(reg.test("PL03AAAAA"));
3)java代碼中使用正則表達(dá)式
? ? 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("日期格式錯(cuò)誤!");
return;
}
System.out.println(matcher.group(1));?
? ? //分組的索引值是從1開始的,所以取第一個(gè)分組的方法是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)驗(yàn)證字符串的格式是否正確
Pattern pattern = Pattern.compile("b*g");
Matcher matcher = pattern.matcher("bbg");
//System.out.println(matcher.matches());
//System.out.println(pattern.matches("b*g", "bbg"));
//驗(yàn)證郵政編碼 ?
//System.out.println(pattern.matches("[0-9]{6}", "200038"));
//System.out.println(pattern.matches("\\d{6}", "200038"));
//驗(yàn)證電話號(hào)碼 ?
System.out.println(pattern.matches("[0-9]{3,4}-?[0-9]+", "02178989799"));
getDate("Nov 10,2009");
//驗(yàn)證***:判斷一個(gè)字符串是不是***號(hào)碼,即是否是15或18位數(shù)字。 ?
System.out.println(pattern.matches("^\\d{15}|\\d{18}$", "123456789009876"));
//2)查詢字符串中滿足條件的子串,并將其替換掉
charReplace();
//3)提前匹配到的子串
getChinese("welcome to china,江西奉新,welcome,你!");
validateEmail("luosijin123@163.com");
}
轉(zhuǎn)載于:https://blog.51cto.com/6817977/1694660
總結(jié)
以上是生活随笔為你收集整理的正则表达式在js和java中的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android读写assets、raw、
- 下一篇: docker 1.8.2 源代码编译