字符c?BR>[abc] a, b, or c (单类) [^abc] 除了a、b或c之外的Q?nbsp;字符Q求反) [a-zA-Z] a到z或A到Z Q包含(范围) [a-z-[bc]] a到zQ除了b和c Q?nbsp;[ad-z]Q减去) [a-z-[m-p]] a到zQ除了m?nbsp;pQ?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]); } } Matcherc?nbsp; Matchercȝ实例用于Ҏl定的字W串序列模式Q对字符?nbsp;列进行匹配。用CharSequence接口把输入提供给匚w器,以便 支持来自多种多样输入源的字符的匹配?BR>通过调用某个模式的matcherҎQ从q个模式生成匚w器?nbsp;匚w器创Z后,可以用它来执行三类不同的匹配操作: matchesҎ试图Ҏ此模式,Ҏ个输入序列进行匹配?nbsp; lookingAtҎ试图Ҏ此模式,从开始处对输入序列进 行匹配?nbsp; findҎ扫描输入序列,L下一个与模式匚w的地斏V?nbsp; q些Ҏ都会q回一个表C成功或p|的布倹{如果匹配成功,通过查询 匚w器的状态,可以获得更多的信?BR>q个c还定义了用新字W串替换匚w序列的方法,q些字符串的内容如果需 要的话,可以从匹配结果推得出?BR>appendReplacementҎ先添加字W串中从当前位置C一?nbsp;匚w位置之间的所有字W,然后d替换倹{appendTaild?nbsp;是字W串中从最后一ơ匹配的位置之后开始,直到l尾的部分?BR>例如Q在字符串blahcatblahcatblah中,W一?nbsp;appendReplacementdblahdog。第二个 appendReplacementdblahdogQ然?nbsp;appendTaildblahQ就生成了: blahdogblahdogblah。请参见CZ 单的单词替换?BR>CharSequence接口 CharSequence接口多不同类型的字符序列提供了统一的只 读访问。你提供要从不同来源搜烦的数据。用String, StringBuffer 和CharBuffer实现CharSequence,Q这样就可以?nbsp;ҎC它们那里获得要搜索的数据。如果这些可用数据源没一个合适的Q你?nbsp;以通过实现CharSequence接口Q编写你自己的输入源?BR>Regex情景范例 以下代码范例演示了java.util.regex软g包在各种常见情Ş 下的用法Q?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()); } } 电子邮g认 以下代码是这样一个例子:你可以检查一些字W是不是一个电子邮件地址?nbsp;它ƈ不是一个完整的、适用于所有可能情形的电子邮g认E序Q但是可以在 需要时加上它?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(); } } 文g查找 /* * 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()); } } l论 现在Java~程语言中的模式匚w和许多其他编E语a一LzM。可以在?nbsp;用程序中使用正则表达式,保数据在输入数据库或发送给应用E序其他部分?nbsp;前,格式是正的Q正则表辑ּq可以用于各U各L理性工作。简而言之, 在Java~程中,可以在Q何需要模式匹配的地方使用正则表达式?/FONT>