String doctyperegex = "^(<!DOCTYPE)"; Pattern doctypepattern = Pattern.compile(doctyperegex, Pattern.CASE_INSENSITIVE); Matcher doctypematcher = doctypepattern.matcher(aline); while(doctypematcher.find()) { System.out.println(aline); aline = "<!DOCTYPE html>"; System.out.println(aline); }先宣告一個正規表達式的字串,名稱為doctyperegex,先將"<!DOCTYPE"這個字串開頭的程式碼找出來,我們只管"<!DOCTYPE"開頭的就可以,後面接什麼東西並不重要,接著把那整句進行取代成"<!DOCTYPE html>",這樣就完成了DOCTYPE的轉換,很簡單吧。同樣的<meta>標籤內容的轉換也是一樣,程式碼片段如下所示:
String metaregex = "^(<meta)"; Pattern metapattern = Pattern.compile(metaregex, Pattern.CASE_INSENSITIVE); Matcher metamatcher = metapattern.matcher(aline); while(metamatcher.find()) { System.out.println(aline); aline = "<meta charset=UTF-8>"; System.out.println(aline); }道理一樣囉~就參考看看吧!
那...這次就先寫到這,接下來還有許多標籤的轉換規則要研究,我還在努力中呢...
沒有留言:
張貼留言