正则表达式和Java编程语言 - 编程入门网
作者 佚名技术
来源 NET编程
浏览
发布时间 2012-06-23
uffer 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());
}
}
电子邮件确认 以下代码是这样一个例子:你可以检查一些字符是不是一个电子邮件地址。 它并不是一个完整的、适用于所有可能情形的电子邮件确认程序,但是可以在 需要时加上它。 /* * 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; while(result) { deletedIllegalChars = true; m.appendReplacement(sb, ""); result = m.find(); } // 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."); } } } 正则表达式和Java编程语言(5)时间:2010-12-13从文件中删除控制字符 /* 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(); } } 正则表达式和Java编程语言(6)时间:2010-12-13文件查找
|
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
上一篇: Java加密技术(五) - 编程入门网下一篇: 优化JAVA性能的几种方法 - 编程入门网
关于正则表达式和Java编程语言 - 编程入门网的所有评论