瀛楃綾?BR>[abc] a, b, or c (綆鍗曠被) [^abc] 闄や簡(jiǎn)a銆乥鎴朿涔嬪鐨勪換鎰?nbsp;瀛楃錛堟眰鍙嶏級(jí) [a-zA-Z] a鍒皕鎴朅鍒癦 錛屽寘鍚紙鑼冨洿) [a-z-[bc]] a鍒皕錛岄櫎浜?jiǎn)b鍜宑 錛?nbsp;[ad-z]錛堝噺鍘伙級(jí) [a-z-[m-p]] a鍒皕錛岄櫎浜?jiǎn)m鍒?nbsp;p錛?nbsp;[a-lq-z] [a-z-[^def]] d, e, 鎴?nbsp;f
public class Splitter { public static void main(String[] args) throws Exception { // Create a pattern to match breaks Pattern p = Pattern.compile(\"[,\\\\s]+\"); // Split input with the pattern String[] result = p.split(\"one,two, three four , five\"); for (int i=0; i<result.length; i++) System.out.println(result[i]); } } Matcher綾?nbsp; Matcher綾葷殑瀹炰緥鐢ㄤ簬鏍規(guī)嵁緇欏畾鐨勫瓧絎︿覆搴忓垪妯″紡錛屽瀛楃搴?nbsp;鍒楄繘琛屽尮閰嶃備嬌鐢–harSequence鎺ュ彛鎶婅緭鍏ユ彁渚涚粰鍖歸厤鍣紝浠ヤ究 鏀寔鏉ヨ嚜澶氱澶氭牱杈撳叆婧愮殑瀛楃鐨勫尮閰嶃?BR>閫氳繃璋冪敤鏌愪釜妯″紡鐨刴atcher鏂規(guī)硶錛屼粠榪欎釜妯″紡鐢熸垚鍖歸厤鍣ㄣ?nbsp;鍖歸厤鍣ㄥ垱寤轟箣鍚庯紝灝卞彲浠ョ敤瀹冩潵鎵ц涓夌被涓嶅悓鐨勫尮閰嶆搷浣滐細(xì) 摟 matches鏂規(guī)硶璇曞浘鏍規(guī)嵁姝ゆā寮忥紝瀵規(guī)暣涓緭鍏ュ簭鍒楄繘琛屽尮閰嶃?nbsp; 摟 lookingAt鏂規(guī)硶璇曞浘鏍規(guī)嵁姝ゆā寮忥紝浠庡紑濮嬪瀵硅緭鍏ュ簭鍒楄繘 琛屽尮閰嶃?nbsp; 摟 find鏂規(guī)硶灝嗘壂鎻忚緭鍏ュ簭鍒楋紝瀵繪壘涓嬩竴涓笌妯″紡鍖歸厤鐨勫湴鏂廣?nbsp; 榪欎簺鏂規(guī)硶閮戒細(xì)榪斿洖涓涓〃紺烘垚鍔熸垨澶辮觸鐨勫竷?yōu)當(dāng)鍊箋傚鏋滃尮閰嶆垚鍔燂紝閫氳繃鏌ヨ 鍖歸厤鍣ㄧ殑鐘舵侊紝鍙互鑾峰緱鏇村鐨勪俊鎭?BR>榪欎釜綾昏繕瀹氫箟浜?jiǎn)鐢ㄦ柊瀛椊W︿覆鏇挎崲鍖歸厤搴忓垪鐨勬柟娉曪紝榪欎簺瀛楃涓茬殑鍐呭濡傛灉闇 瑕佺殑璇濓紝鍙互浠庡尮閰嶇粨鏋滄帹綆楀緱鍑恒?BR>appendReplacement鏂規(guī)硶鍏堟坊鍔犲瓧絎︿覆涓粠褰撳墠浣嶇疆鍒頒笅涓涓?nbsp;鍖歸厤浣嶇疆涔嬮棿鐨勬墍鏈夊瓧絎︼紝鐒跺悗娣誨姞鏇挎崲鍊箋俛ppendTail娣誨姞鐨?nbsp;鏄瓧絎︿覆涓粠鏈鍚庝竴嬈″尮閰嶇殑浣嶇疆涔嬪悗寮濮嬶紝鐩村埌緇撳熬鐨勯儴鍒嗐?BR>渚嬪錛屽湪瀛楃涓瞓lahcatblahcatblah涓紝絎竴涓?nbsp;appendReplacement娣誨姞blahdog銆傜浜屼釜 appendReplacement娣誨姞blahdog錛岀劧鍚?nbsp;appendTail娣誨姞blah錛屽氨鐢熸垚浜?jiǎn)锛?xì) blahdogblahdogblah銆傝鍙傝紺轟緥 綆鍗曠殑鍗曡瘝鏇挎崲銆?BR>CharSequence鎺ュ彛 CharSequence鎺ュ彛涓鴻澶氫笉鍚岀被鍨嬬殑瀛楃搴忓垪鎻愪緵浜?jiǎn)缁熶竴鐨勫彧 璇昏闂備綘鎻愪緵瑕佷粠涓嶅悓鏉ユ簮鎼滅儲(chǔ)鐨勬暟鎹傜敤String, StringBuffer 鍜孋harBuffer瀹炵幇CharSequence,錛岃繖鏍峰氨鍙互寰?nbsp;瀹規(guī)槗鍦頒粠瀹冧滑閭i噷鑾峰緱瑕佹悳绱㈢殑鏁版嵁銆傚鏋滆繖浜涘彲鐢ㄦ暟鎹簮娌′竴涓悎閫傜殑錛屼綘鍙?nbsp;浠ラ氳繃瀹炵幇CharSequence鎺ュ彛錛岀紪鍐欎綘鑷繁鐨勮緭鍏ユ簮銆?BR>Regex鎯呮櫙鑼冧緥 浠ヤ笅浠g爜鑼冧緥婕旂ず浜?jiǎn)java.util.regex杞歡鍖呭湪鍚勭甯歌鎯呭艦 涓嬬殑鐢ㄦ硶錛?BR>綆鍗曠殑鍗曡瘝鏇挎崲 /* * This code writes \"One dog, two dogs in the yard.\" * to the standard-output stream: */ import java.util.regex.*;
public class Replacement { public static void main(String[] args) throws Exception { // Create a pattern to match cat Pattern p = Pattern.compile(\"cat\"); // Create a matcher with an input string Matcher m = p.matcher(\"one cat,\" + \" two cats in the yard\"); StringBuffer sb = new StringBuffer(); boolean result = m.find(); // Loop through and create a new String // with the replacements while(result) { m.appendReplacement(sb, \"dog\"); result = m.find(); } // Add the last segment of input to // the new String m.appendTail(sb); System.out.println(sb.toString()); } } 鐢?shù)瀛愰偖錃g紜 浠ヤ笅浠g爜鏄繖鏍蜂竴涓緥瀛愶細(xì)浣犲彲浠ユ鏌ヤ竴浜涘瓧絎︽槸涓嶆槸涓涓數(shù)瀛愰偖浠跺湴鍧銆?nbsp;瀹冨茍涓嶆槸涓涓畬鏁寸殑銆侀傜敤浜庢墍鏈夊彲鑳芥儏褰㈢殑鐢?shù)瀛愰偖錃g紜紼嬪簭錛屼絾鏄彲浠ュ湪 闇瑕佹椂鍔犱笂瀹冦?BR>/* * Checks for invalid characters * in email addresses */ public class EmailValidation { public static void main(String[] args) throws Exception {
String input = \"@sun.com\"; //Checks for email addresses starting with //inappropriate symbols like dots or @ signs. Pattern p = Pattern.compile(\"^\\\\.|^\\\\@\"); Matcher m = p.matcher(input); if (m.find()) System.err.println(\"Email addresses don\'t start\" + \" with dots or @ signs.\"); //Checks for email addresses that start with //www. and prints a message if it does. p = Pattern.compile(\"^www\\\\.\"); m = p.matcher(input); if (m.find()) { System.out.println(\"Email addresses don\'t start\" + \" with \\\"www.\\\", only web pages do.\"); } p = Pattern.compile(\"[^A-Za-z0-9\\\\.\\\\@_\\\\-~#]+\"); m = p.matcher(input); StringBuffer sb = new StringBuffer(); boolean result = m.find(); boolean deletedIllegalChars = false;
// Add the last segment of input to the new String m.appendTail(sb);
input = sb.toString();
if (deletedIllegalChars) { System.out.println(\"It contained incorrect characters\" + \" , such as spaces or commas.\"); } } } 浠庢枃浠朵腑鍒犻櫎鎺у埗瀛楃 /* This class removes control characters from a named * file. */ import java.util.regex.*; import java.io.*;
public class Control { public static void main(String[] args) throws Exception {
//Create a file object with the file name //in the argument: File fin = new File(\"fileName1\"); File fout = new File(\"fileName2\"); //Open and input and output stream FileInputStream fis = new FileInputStream(fin); FileOutputStream fos = new FileOutputStream(fout);
BufferedReader in = new BufferedReader( new InputStreamReader(fis)); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(fos));
// The pattern matches control characters Pattern p = Pattern.compile(\"{cntrl}\"); Matcher m = p.matcher(\"\"); String aLine = null; while((aLine = in.readLine()) != null) { m.reset(aLine); //Replaces control characters with an empty //string. String result = m.replaceAll(\"\"); out.write(result); out.newLine(); } in.close(); out.close(); } } 鏂囦歡鏌ユ壘 /* * Prints out the comments found in a .java file. */ import java.util.regex.*; import java.io.*; import java.nio.*; import java.nio.charset.*; import java.nio.channels.*;
public class CharBufferExample { public static void main(String[] args) throws Exception { // Create a pattern to match comments Pattern p = Pattern.compile(\"http://.*$\", Pattern.MULTILINE);
// Get a Channel for the source file File f = new File(\"Replacement.java\"); FileInputStream fis = new FileInputStream(f); FileChannel fc = fis.getChannel();
// Get a CharBuffer from the source file ByteBuffer bb = fc.map(FileChannel.MAP_RO, 0, (int)fc.size()); Charset cs = Charset.forName(\"8859_1\"); CharsetDecoder cd = cs.newDecoder(); CharBuffer cb = cd.decode(bb);
// Run some matches Matcher m = p.matcher(cb); while (m.find()) System.out.println(\"Found comment: \"+m.group()); } } 緇撹 鐜板湪Java緙栫▼璇█涓殑妯″紡鍖歸厤鍜岃澶氬叾浠栫紪紼嬭璦涓鏍風(fēng)伒媧諱簡(jiǎn)銆傚彲浠ュ湪搴?nbsp;鐢ㄧ▼搴忎腑浣跨敤姝e垯琛ㄨ揪寮忥紝紜繚鏁版嵁鍦ㄨ緭鍏ユ暟鎹簱鎴栧彂閫佺粰搴旂敤紼嬪簭鍏朵粬閮ㄥ垎涔?nbsp;鍓嶏紝鏍煎紡鏄紜殑錛屾鍒欒〃杈懼紡榪樺彲浠ョ敤浜庡悇縐嶅悇鏍風(fēng)殑綆$悊鎬у伐浣溿傜畝鑰岃█涔嬶紝 鍦↗ava緙栫▼涓紝鍙互鍦ㄤ換浣曢渶瑕佹ā寮忓尮閰嶇殑鍦版柟浣跨敤姝e垯琛ㄨ揪寮忋?/FONT>