快速业务通道

正则表达式和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

文件查找

/* * Prints out the comments found in a .java file. */ import

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号