Java regular expression to find and replace specified characters exactly

situation:

        When copying a model, add "-copy" to the name. In the beginning, replace was used to replace all. During the test, it appears that the name of the element is the same as the name of the model, so there is a problem with the replacement. At this time, it should be changed to a regular expression to match and replace the corresponding content.

{
	"flowModel": {
		"flowMod": 502,
		"flowType": 2,
		"flowTypeName": "普通流程",
		"flowTypeNameUs": "common",
		"flowName": "流程表单模型A",
		"flowNameUs": "流程表单模型A",
		"fieldName": "流程表单模型A",
		"state": "A"
	},
	"formModel": {
		"formId": 108,
		"flowMod": 502,
		"flowName": "流程表单模型A",
		"flowNameUs": "流程表单模型A",
		"formName": "流程表单模型A",
		"formNameUs": "流程表单模型A",
		"fieldName": "流程表单模型A"
	}
}

Goal: Replace the names inside flowName, flowNameUs, formName and formNameUs

deal with one first

Write the regular form first: 

String pattern ="\"flowName\""+ ":"+"\\s*\"" + modelName + "\"";

\\s* can match 0 or more spaces

After matching the corresponding value, replace it with the entire value

public static String matchReplaceName(String content, String modelName, String newModelName)
    {
        String pattern ="\"flowName\""+ ":"+"\\s*\"" + modelName + "\"";
        System.out.println("pattern "+pattern);
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(content);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String group = m.group();
            m.appendReplacement(sb, group.replace(modelName, newModelName));
        }
        m.appendTail(sb);
        return sb.toString();
    }

test:


    public static void main(String[] args) {
        String content = initData();
        String modelName = "流程表单模型A";
        String newModelName = "流程表单模型A-复制";
        content = matchReplaceName(content, modelName, newModelName);
        System.out.println(content);
    } 

    private static String initData(){
        String data = "{\n" +
                "\t\"flowModel\": {\n" +
                "\t\t\"flowMod\": 502,\n" +
                "\t\t\"flowType\": 2,\n" +
                "\t\t\"flowTypeName\": \"普通流程\",\n" +
                "\t\t\"flowTypeNameUs\": \"common\",\n" +
                "\t\t\"flowName\": \"流程表单模型A\",\n" +
                "\t\t\"flowNameUs\": \"流程表单模型A\",\n" +
                "\t\t\"fieldName\": \"流程表单模型A\",\n" +
                "\t\t\"state\": \"A\"\n" +
                "\t},\n" +
                "\t\"formModel\": {\n" +
                "\t\t\"formId\": 108,\n" +
                "\t\t\"flowMod\": 502,\n" +
                "\t\t\"flowName\": \"流程表单模型A\",\n" +
                "\t\t\"flowNameUs\": \"流程表单模型A\",\n" +
                "\t\t\"formName\": \"流程表单模型A\",\n" +
                "\t\t\"formNameUs\": \"流程表单模型A\",\n" +
                "\t\t\"fieldName\": \"流程表单模型A\"\n" +
                "\t}\n" +
                "}";
        return data;
    }

result:

pattern "flowName":\s*"流程表单模型A"
{
	"flowModel": {
		"flowMod": 502,
		"flowType": 2,
		"flowTypeName": "普通流程",
		"flowTypeNameUs": "common",
		"flowName": "流程表单模型A-复制",
		"flowNameUs": "流程表单模型A",
		"fieldName": "流程表单模型A",
		"state": "A"
	},
	"formModel": {
		"formId": 108,
		"flowMod": 502,
		"flowName": "流程表单模型A-复制",
		"flowNameUs": "流程表单模型A",
		"formName": "流程表单模型A",
		"formNameUs": "流程表单模型A",
		"fieldName": "流程表单模型A"
	}
}

many:

expression:

Completed single, multiple separated by "|", first continue to write single, and then spliced ​​together

     public static String matchReplaceName(String content, String modelName, String newModelName)
    {
        String condition = ":"+"\\s*\"" + modelName + "\"";
        String pattern1 = "\"flowName\""+condition;
        String pattern2 = "\"flowNameUs\""+condition;
        String pattern3 = "\"formName\""+condition;
        String pattern4 = "\"formNameUs\""+condition;
        String pattern = pattern1 +"|"+pattern2+"|"+ pattern3 +"|"+pattern4;
        System.out.println("pattern "+pattern);
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(content);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String group = m.group();
            group = group.replace(modelName, newModelName);
            m.appendReplacement(sb, group);
        }
        m.appendTail(sb);
        return sb.toString();
    }

Finally, the output of pattern can be made into a whole line

Test Results:

pattern "flowName":\s*"流程表单模型A"|"flowNameUs":\s*"流程表单模型A"|"formName":\s*"流程表单模型A"|"formNameUs":\s*"流程表单模型A"
{
	"flowModel": {
		"flowMod": 502,
		"flowType": 2,
		"flowTypeName": "普通流程",
		"flowTypeNameUs": "common",
		"flowName": "流程表单模型A-复制",
		"flowNameUs": "流程表单模型A-复制",
		"fieldName": "流程表单模型A",
		"state": "A"
	},
	"formModel": {
		"formId": 108,
		"flowMod": 502,
		"flowName": "流程表单模型A-复制",
		"flowNameUs": "流程表单模型A-复制",
		"formName": "流程表单模型A-复制",
		"formNameUs": "流程表单模型A-复制",
		"fieldName": "流程表单模型A"
	}
}

 

Summarize:

        If you want to replace specified characters precisely, you can consider regularization, \\s* can match 0 or more spaces, and the applicability of replace will be better. Start with a single one, and then increase and verify one by one, so that problems can be found and debugged more easily.

Guess you like

Origin blog.csdn.net/qq_35461948/article/details/130827637