一个Java语言编写的,批量替换纯文本文件内容。
使用说明:
1. 需要修改的文件,修改内容都有共同点。
2. 可以捕捉到被替换的内容,在被替换的内容上可以新增加自己的内容进行替换。
3. 工具并没有提供相关的操作界面,只能通过main方法执行。
4. 程序可以在原有的基础上进行扩展。
5. 开发环境为: JDK1.7
其中一段相关代码:
package com.what21.tool.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyRegex {
private String context;
private String regex;
private Pattern pattern;
/**
* @param regex
*/
public MyRegex(String regex) {
this.regex = regex;
this.pattern = Pattern.compile(regex);
}
/**
* @return
*/
public String toMatchText() {
Matcher matcher = getPattern().matcher(getContext());
if (matcher.find()) {
return matcher.group();
}
return null;
}
/**
* @return
*/
public boolean toMatches() {
Matcher matcher = getPattern().matcher(getContext());
return matcher.matches();
}
/**
* @param text
* @return
*/
public String toReplaceText(String text) {
return getContext().replaceFirst(getRegex(), text);
}
/**
* @param text
* @return
*/
public String toAllReplaceText(String text) {
return getContext().replaceAll(getRegex(), text);
}
/**
* @return
*/
public String getContext() {
return context;
}
/**
* @param context
*/
public void setContext(String context) {
this.context = context;
}
/**
* @return
*/
public String getRegex() {
return regex;
}
/**
* @return
*/
public Pattern getPattern() {
return pattern;
}
}
密码:0jg5