import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
/**
*
- @author xxx
- @version Created:2019年6月6日
*/
public class ReplaceLabelTest {
@Test
public void testReplaceLabel() {
String xml = "MuMMMMMMMMMMMMMMMMM<camunda:formField id=\"FormField_2qkoval\" label=\"ZIDUAN\" type=\"string\" defaultValue=\"whethershow\">";
String regex = "(.*)<camunda:formField(.*)label=";
String newXml = replaceLabel(regex, xml, 6, "name=");
System.out.println(newXml);
}
/*****
* 替换xml文件中指定<camunda:formField标签中的label属性为name
*
* @param regex
* @param xml
* @return
*/
public static String replaceLabel(String regex, String xml, int toReplaceStrLength, String toReplaceStr) {
if(StringUtils.isEmpty(regex) || StringUtils.isEmpty(xml)) {
return null;
}
System.out.println(xml);
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(xml);
m.find();
int end = m.end();
int start = end - toReplaceStrLength;
//String subStr = xml.substring(start, end-1);
//System.out.println("substr: " + subStr);
StringBuffer buffer = new StringBuffer(xml);
buffer.replace(start, end, toReplaceStr);
System.out.println(buffer.toString());
return buffer.toString();
}
}