⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 regexappend.java

📁 java nio 编程一个实例子.服务端程序
💻 JAVA
字号:
package com.ronsoft.books.nio.regex;import java.util.regex.Pattern;import java.util.regex.Matcher;/** * Test the appendReplacement() and appendTail() methods of the * java.util.regex.Matcher class. * Created: Dec 28, 2001 * * @author Ron Hitchens (ron@ronsoft.com) * @version $Id: RegexAppend.java,v 1.5 2002/04/11 02:58:04 ron Exp $ */public class RegexAppend{	public static void main (String [] argv)	{		String input = "Thanks, thanks very much";		String regex = "([Tt])hanks";		Pattern pattern = Pattern.compile (regex);		Matcher matcher = pattern.matcher (input);		StringBuffer sb = new StringBuffer();		// loop while matches are encountered		while (matcher.find()) {			if (matcher.group(1).equals ("T")) {				matcher.appendReplacement (sb, "Thank you");			} else {				matcher.appendReplacement (sb, "thank you");			}		}		// complete the transfer to the StringBuffer		matcher.appendTail (sb);		// print the result		System.out.println (sb.toString());		// Let's try that again, using the $n escape in the replacement		sb.setLength (0);		matcher.reset();		String replacement = "$1hank you";		// loop while matches are encountered		while (matcher.find()) {			matcher.appendReplacement (sb, replacement);		}		// complete the transfer to the StringBuffer		matcher.appendTail (sb);		// print the result		System.out.println (sb.toString());		// And once more, the easy way (because this example is simple)		System.out.println (matcher.replaceAll (replacement));		// One last time, using only the String		System.out.println (input.replaceAll (regex, replacement));			}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -