自己写一个java程序研究对{key}的匹配和替换

很多工具类都有对{key}进行匹配和替换的功能,但是限于其功能并不能适应特殊业务要求,所以自己写程序研究其原理并对其功能进行扩展。
这里我写了一个类MatchReplace,包含三个方法
1、replaceFirst(String source, Map<String, String> params)
只替换第一个{key}中的内容,{}中内容首尾的空白字符去除之后作为key
但是key中的空白字符不做处理
即使第一个{key}替换失败,后面的{key}也不做处理
2、replaceAll(String source, Map<String, String> params)
对字符串中的所有{key}进行替换,但是key中含有的空白字符不做处理
比如{ key    }可以被(key,value)替换,但是{ k ey}则不能被(key,value)替换,只能被(k ey,value)替换
3、replaceAllClearBlanks(String source, Map<String, String> params)
对字符串中的所有{key}进行替换,并且会清除key中包含的空白字符之后再进行匹配
比如{ key    }和{ k ey}均能被(key,value)替换,但是均不能被(k ey,value)替换
所以使用此方法对map的要求是key中不能包含任何空白字符

既然自己可以根据实际业务对类的功能进行扩展和优化
所以当遇到需要对模板文件进行值替换的时候就相当好用了
只需要再写一个工具类在读写文件的时候调用该类的方法即可

MatchReplace的源代码

package per.test.string;

import java.util.Map;

/**
 * 对字符串进行模式化替换
 * 将{key}替换成key对应的value
 * @author Mr_ZhangSan
 *
 */
public class MatchReplace {

	/**
	 * 只替换第一个{key}中的内容,{}中内容首尾的空白字符去除之后作为key
	 * 但是key中的空白字符不做处理
	 * 即使第一个{key}替换失败,后面的{key}也不做处理
	 * @param source
	 * @param params
	 * @return
	 */
	public static String replaceFirst(String source, Map<String, String> params) {
		int begin = source.indexOf("{");
		int end = source.indexOf("}");
		//source存在"{",并且"{"的位置在"}"之前时进行替换
		if((begin > -1) && (begin < end)) {
			//取出{key}中的可以并去除首尾的空白字符
			String sub = source.substring(begin + 1, end);
			sub = sub.trim();	//去除首尾空白字符
			//System.out.println(sub);
			if(params.containsKey(sub)) {
				//连同{和}一起进行替换
				String regex = source.substring(begin, end + 1);
				source = source.replace(regex, params.get(sub));
			}
		}
		return source;
	}
	
	/**
	 * 对字符串中的所有{key}进行替换,但是key中含有的空白字符不做处理
	 * @param source
	 * @param params
	 * @return
	 */
	public static String replaceAll(String source, Map<String, String> params) {
		int begin = source.indexOf("{");
		int end = 0;
		String sub = "";
		while(begin > -1) {
			//end = source.indexOf("}");	//如果用此方式可能造成死循环
			//并不能保证每个{key}都会被替换掉,所以需要从上次的{之后进行匹配
			end = source.indexOf("}", begin);
			if(begin < end) {
				sub = source.substring(begin + 1, end);
				//sub = sub.replaceAll("\\s*", "");	//去除所有空白字符
				sub = sub.trim();	//去除首尾空白字符
				if(params.containsKey(sub)) {
					String regex = source.substring(begin, end + 1);
					source = source.replace(regex, params.get(sub));
				}
				begin = source.indexOf("{", begin+1);	//如此取值方为合理
			}
			//begin = source.indexOf("{");	//如果用此方式可能造成死循环
		}
		return source;
	}
	
	/**
	 * 对字符串中的所有{key}进行替换,并且会清除key中包含的空白字符之后再进行匹配
	 * @param source
	 * @param params
	 * @return
	 */
	public static String replaceAllClearBlanks(String source, Map<String, String> params) {
		int begin = source.indexOf("{");
		int end = 0;
		String sub = "";
		while(begin > -1) {
			//end = source.indexOf("}");	//如果用此方式可能造成死循环
			//并不能保证每个{key}都会被替换掉,所以需要从上次的{之后进行匹配
			end = source.indexOf("}", begin);
			if(begin < end) {
				sub = source.substring(begin + 1, end);
				sub = sub.replaceAll("\\s*", "");	//去除所有空白字符
				//sub = sub.trim();	//去除首尾空白字符
				if(params.containsKey(sub)) {
					String regex = source.substring(begin, end + 1);
					source = source.replace(regex, params.get(sub));
				}
				begin = source.indexOf("{", begin+1);	//如此取值方为合理
			}
			//begin = source.indexOf("{");	//如果用此方式可能造成死循环
		}
		return source;
	}

}

测试类MatcherTest的测试代码

package per.test.string;

import java.util.HashMap;

public class MatcherTest {
	
	public static void main(String[] args) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("key1", "hello");
		params.put("key2", "world");
		params.put("ke y", "java");
		String source = "1 {  ke y1} {	key2	 }2";
		String targetFirst = MatchReplace.replaceFirst(source, params);
		String targetAll = MatchReplace.replaceAll(source, params);
		String targetClearBlanks = MatchReplace.replaceAllClearBlanks(source, params);
		System.out.println(targetFirst);
		System.out.println(MatchReplace.replaceFirst("1 {  key1} {	key2	 }2", params));
		System.out.println(targetAll);
		//如果MatchReplace使用begin = source.indexOf("{"),因为{key}不能被处理掉,所以会死循环
		System.out.println(MatchReplace.replaceAll("{key1} {key} {key2}", params));
		System.out.println(MatchReplace.replaceAll("{key1} {ke y} {key2}", params));
		System.out.println(targetClearBlanks);
		System.out.println(MatchReplace.replaceAllClearBlanks("1 {  key1} {	key2	 }2", params));
		System.out.println(MatchReplace.replaceAllClearBlanks("{key1} {key} {key2}", params));
		System.out.println(MatchReplace.replaceAllClearBlanks("{key1} {ke y} {key2}", params));
	}
}

测试结果

1 {  ke y1} {	key2	 }2
1 hello {	key2	 }2
1 {  ke y1} world2
hello {key} world
hello java world
1 hello world2
1 hello world2
hello {key} world
hello {ke y} world
发布了46 篇原创文章 · 获赞 10 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/sqhren626232/article/details/96868611