Regex的尋找;主要是用到String類中的split();
String str;
str.split();方法中傳入按照什麼規則截取,返回一個String數組
常見的截取規則:
str.split("\\.")按照.來截取
str.split(" ")按照空格截取
str.split("cc+")按照c字元來截取,2個c或以上
str.split((1)\\.+)按照字串中含有2個字元或以上的地方截取(1)表示分組為1
截取的例子;
按照分組截取;截取的位置在兩個或兩個以上的地方
String str = "publicstaticccvoidddmain"; //對錶達式進分組重用 String ragex1="(.)\\1+"; String[] ss = str.split(ragex1); for(String st:ss){ System.out.println(st); }//按照兩個cc+來截取String ragex = " "; //切割 String strs = "publicstaticccvoidddmain"; String ragexs = "cc+"; String[] s = strs.split(ragexs); for(String SSSS :s){ System.out.println(SSSS); } System.out.println("=-=========");
Regex中的替換;
文法定義規則;
String s =str.replaceAll(ragex, newstr);
字串中的替換是replace();
將4個或4個以上的連續的數字替換成*
// 替換 String str="wei232123jin234"; String ragex = "\\d{4,}"; String newstr = "*"; String s =str.replaceAll(ragex, newstr); System.out.println(s);
將重複的字串換成一個*
String str ="wwweiei222222jjjiiinnn1232"; String ragex ="(.)\\1+"; String newStr ="*"; String s = str.replaceAll(ragex, newStr); System.out.println(s);
將 我...我...要..要.吃...吃...飯 換成 我要吃飯
String str = "我...我...要..要.吃...吃...飯"; String regex = "\\.+"; String newStr = ""; str=test(str, regex, newStr); regex = "(.)\\1+"; newStr = "$1"; test(str, regex, newStr);public static String test(String str, String regex, String newStr) { String str2 = str.replaceAll(regex, newStr); System.out.println(str2); return str2; }
Regex的字串的擷取
1,根據定義的Regex建立Pattern對象
2,使用Pattern對象類匹配
3,判斷是否為true
4,加入到組
例子;
String str = "public static void main(String[] args)" + " public static void main(String[] args)public static void main(String[] args)"; String ragex = "\\b[a-zA-Z]{4,5}\\b"; Pattern p =Pattern.compile(ragex); Matcher m = p.matcher(str); while(m.find()){ String s = m.group(); System.out.println(s); }
作業:
1,擷取<html>user</user>中的user
String str ="<html>user</html>";String regex = "<html>|</html>"; String newStr = ""; String str2 = str.replaceAll(regex, newStr);System.out.println(str2);
2,擷取dhfjksaduirfn 11@qq.com dsjhkfa wang@163.com wokaz中的郵箱號碼
String regex = " "; String[] strs=str.split(regex); for(String str2:strs){ String ragexDemo = "[a-zA-Z0-9]([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*" + "@([a-zA-Z0-9]+)\\.[a-zA-Z]+\\.?[a-zA-Z]{0,2}";Pattern p = Pattern.compile(ragexDemo);Matcher m = p.matcher(str2);while(m.find()){System.out.println(m.group()); } }
範例程式碼:
import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;public class test { public static void main(String[] args) { getStrings(); //用Regex擷取指定字串內容中的指定內容 System.out.println("********************"); replace(); //用Regex替換字串內容 System.out.println("********************"); strSplit(); //使用Regex切割字串 System.out.println("********************"); strMatch(); //字串匹配 } private static void strMatch() { String phone = "13539770000"; //檢查phone是否是合格的手機號(標準:1開頭,第二位為3,5,8,後9位為任一數字) System.out.println(phone + ":" + phone.matches("1[358][0-9]{9,9}")); //true String str = "abcd12345efghijklmn"; //檢查str中間是否包含12345 System.out.println(str + ":" + str.matches("\\w+12345\\w+")); //true System.out.println(str + ":" + str.matches("\\w+123456\\w+")); //false } private static void strSplit() { String str = "asfasf.sdfsaf.sdfsdfas.asdfasfdasfd.wrqwrwqer.asfsafasf.safgfdgdsg"; String[] strs = str.split("\\."); for (String s : strs){ System.out.println(s); } } private static void getStrings() { String str = "rrwerqq84461376qqasfdasdfrrwerqq84461377qqasfdasdaa654645aafrrwerqq84461378qqasfdaa654646aaasdfrrwerqq84461379qqasfdasdfrrwerqq84461376qqasfdasdf"; Pattern p = Pattern.compile("qq(.*?)qq"); Matcher m = p.matcher(str); ArrayList<String> strs = new ArrayList<String>(); while (m.find()) { strs.add(m.group(1)); } for (String s : strs){ System.out.println(s); } } private static void replace() { String str = "asfas5fsaf5s4fs6af.sdaf.asf.wqre.qwr.fdsf.asf.asf.asf"; //將字串中的.替換成_,因為.是特殊字元,所以要用\.表達,又因為\是特殊字元,所以要用\\.來表達. str = str.replaceAll("\\.", "_"); System.out.println(str); }}