PHP正则表达式 |
|
来源:互联网 作者:未知 发布时间:2007-12-08 |
|
|
Regex情景范例
以下代码范例演示了java.util.regex软件包在各种常见情形 下的用法:
简单的单词替换
/* * 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()); php程序员站 } }
电子邮件确认
以下代码是这样一个例子:你可以检查一些字符是不是一个电子邮件地址。 它并不是一个完整的、适用于所有可能情形的电子邮件确认程序,但是可以在 需要时加上它。
/* * 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\\."); php程序员站 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."); } } }
从文件中删除控制字符
/* This class removes control characters from a named * file. */ import java.util.regex.*; www.phperz.com 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; phperz.com 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("//.*$", Pattern.MULTILINE);
// Get a Channel for the source file File f = new File("Replacement.java"); FileInputStream fis = new FileInputStream(f); FileChannel fc = fis.getChannel();
|
上一页 1 2 34 下一页 |
[收藏此页] [打印本页] [返回顶部] |
|
|
|
|
|
|
|
|
|