黑馬程式員—正規表達式概念
---------------------- <a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">.Net教育訓練</a>、期待與您交流! ------
一.概念:
正規表達式:用于專門操作字元串。可以簡化對隻付出的複雜操作。
1.特點:通過特點的字元來展現的, [^abc] ,[ \t\n\x0B\f\r]
2.弊端:掌握正規表達式必須學習一些符合,定義越多 閱讀性差,而且不好記憶了解容易混淆。
package com.itheima.regex.demo;
public class RegexDemo {
public static void main(String[] args) {
String id="42112419890304";
//checkId(id);
checkId2(id);
}
public static void checkId(String id){
int len=id.length(); //擷取長度
if(len>=10 && len<=18){
if(id.startsWith("0")){
System.out.println("編号不能以0開頭");
}else{
try {
long Id=Long.getLong(id);
System.out.print("id編号是:"+Id);
} catch (Exception e) {
System.out.println("出現非法字元。");
}
}
}else{
System.out.println("身份證編号長度不符合要求");
}
}
//使用正規表達式判斷
public static void checkId2(String id) {
// 定義正規表達式規則:第一位是1-9中的一個,然後0-9中的數字出現10-17次。
String regex = "[1-9][0-9]{9,17}" ; //定義規則
boolean flag = id.matches(regex) ;
if (flag)
System.out.println(id+":此号碼正确");
else
System.out.println(id+":此編号不合法");
}
}
二、正規表達式具體操作:
package com.itheima.regex.demo;
public class RegexDemo2 {
public static void main(String[] args) {
//functionDmeo1();
//functionDmeo2();
functionDmeo3();
}
//比對手機号碼:通常手機号碼是11位,而開頭一般是13、15、18。
public static void functionDmeo1(){
String tel="13632979740";
String regex="1[358]\\d{9}"; //\d 數字:[0-9]
Boolean b=tel.matches(regex);
System.out.print("手機号碼:"+b);
}
//切割: String[] split(String regex) ; 按照切割規則将字元串切割成多個子串。
// 按照定義的規則進行切割
public static void functionDmeo2(){
String str="hao123ddwww360qq";
String [] names=str.split("(.)\\1+");
for(String name:names){
System.out.print(name);
}
}
//替換 replace
public static void functionDmeo3(){
String str="hao123ddwww360qq";
str=str.replaceAll("(.)\\1+", "$1");
System.out.print(str);
}
}
1、比對: boolean matches(String regex) 用規則比對整個字元串,隻要有一處不符合規則就傳回false。
總結:如果隻想知道該字元串是對是錯,使用比對。
2、切割: String[] split(String regex) ; 按照切割規則将字元串切割成多個子串。
總結:想要按照自定的方式将字元串變成多個字元串,切割。擷取規則以外的子串。
3、替換:boolean replaceAll(String regex, String replacement) 使用給定的 replacement 替換此字元串所有比對給定的正規表達式的子字元串。
總結:想要将已有的字元串變成另一個字元串, str=str.replaceAll("(.)\\1+", "$1")則使用替換。
練習題1:将字元串轉換為:我要學程式設計開發
"我我我我...我要...要要要...學學..學編..編編編編編編編....程式設計.程...程程程..程.....開開開....發發發.發"
思路:先去掉".",然後将重疊字替換。
public static void Test_1(){
String str="我我我我...我要...要要要...學學..學編..編編編編編編編....程式設計.程...程程程..程.....開開開....發發發.發";
//将字元中的.去掉,用替換
str=str.replaceAll("\\.+", "");
//System.out.println(str);
//2.替換相同的疊詞
str=str.replaceAll("(.)\\1+", "$1");
System.out.println(str);
}
練習題2:将IP位址進行排序 如: 192.168.1.234 102.49.23.13 16.10.21.10 1.1.1.1 8.109.90.55
分析: 1、讓IP每一段位數相同,按照每一段需要的最多的0進行補齊,是以每一位至少有3位。
2、然後将每一段隻保留3位。
3、切割出每一個IP位址,去掉0開頭。
public static void Test_2(){
String ip_str="192.168.1.234 102.49.23.13 16.10.21.10 10.2.36.72 8.109.90.55";
//1.先補零,最多補2個
ip_str=ip_str.replaceAll("(\\d+)", "00$1");
System.out.println("每段補齊0後:"+ip_str);
//00192.00168.001.00234 00102.0049.0023.0013 0016.0010.0021.0010 0010.002.0036.0072 008.00109.0090.0055
//2。每段補齊後保留3位數
ip_str= ip_str.replaceAll("0*(\\d{3})", "$1") ;
System.out.println("去0後:"+ip_str);
//3.切割,将Ip切出
String [] ips=ip_str.split(" +");
TreeSet<String> ts=new TreeSet<String>();
for(String ip:ips){
ts.add(ip);
}
for(String ip:ts){
System.out.println(ip.replaceAll("0*(\\d+)", "$1"));
}
}
每段補齊0後:00192.00168.001.00234 00102.0049.0023.0013 0016.0010.0021.0010 0010.002.0036.0072 008.00109.0090.0055
去0後:192.168.001.234 102.049.023.013 016.010.021.010 010.002.036.072 008.109.090.055
8.109.90.55
10.2.36.72
16.10.21.10
102.49.23.13
192.168.1.234
正規表達式總結:正規表達式符号比較多,容易混淆,前幾天敲的估計過一段時間忘記,這個開發中前段Javascript判斷登入驗證應該用的比較多,學習的是一種思路,這表達式符号用的時候可以查閱,死記硬背是沒用用的,而且是痛苦的。
---------------------- <a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">ASP.Net+Unity開發</a>、<a href="http://www.itheima.com" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" target="blank">.Net教育訓練</a>、期待與您交流! -------------------------------