简体   繁体   中英

Replacing a line in java from a substring using regex

Say I have a multiple files which look like follows, and I'm looping through storing them in a variable called 'text':

<Property name="FirstProp"><![CDATA[  ]]></PackageProperty>
<Property name="SecondProp"><![CDATA[  ]]></PackageProperty>
<Property name="ThirdProp"><![CDATA[  ]]></PackageProperty>

Some of the files can have CDATA in them, so the only thing each has in common is the property name. I've stored the PropertyNames in a String array called propNames[ ]. So, "SecondProp" would be propNames[1].

I now want to replace the second line to include some CDATA, I've tried this but it doesn't work because I only want it to look before and after the current line, not the entire file.

String CDATAReplacement = "<Property name=\"SecondProp\"><![CDATA[ Some Value ]]></PackageProperty>";
text.replaceAll( "(.*)"+propNames[1]+"(.*)", CDATAReplacement )

End result:

<Property name="FirstProp"><![CDATA[  ]]></PackageProperty>
<Property name="SecondProp"><![CDATA[ Some Value ]]></PackageProperty>
<Property name="ThirdProp"><![CDATA[  ]]></PackageProperty>

I could do this quite easily using sed and execute the script within the java program, but I know it's bad practise so a Java solution would be better.

I would recommend you to use JDOM Library to work with XML: http://www.studytrails.com/java/xml/jdom2/java-xml-jdom2-xpath.jsp . So you can work through XPath instead of regexps .

First of all since it's xml I would recommend a xml manipulation library. However if you still want to use regex, I would recommend atleast validating the line a bit more so you dont end up replacing a line which happens to have the the name "SecondProp" in its value, or as part of its own name.

If you loaded the file normally with line endings, your code should however work. By default regular expressions in java are limited lines, Multi-line mode is off by default.

Here's a an example of a better matching regular expression though.

String regex =  "<property name=\"" + propertyName + "\"><!\\[CDATA\\[" + "([\s]*)" + "\\]\\]>" + "</packageProperty>";

Example:

String line = "<property name=\"SecondProp\"><![CDATA[   ]]></packageProperty>";
String propertyname = "SecondProp";
String CDATAReplacement = "<Property name=\"SecondProp\"><![CDATA[ Some Value ]]></PackageProperty>";
String regex =  "<property name=\"" + propertyName + "\"><!\\[CDATA\\[" + "([\s]*)" + "\\]\\]>" + "</packageProperty>";
System.out.println(line);
System.out.println(line.replaceAll(regex,CDATAReplacement));
String test = "<Property name=\"FirstProp\"><![CDATA[  ]]></PackageProperty>\n<Property name=\"SecondProp\"><![CDATA[  ]]></PackageProperty>\n<Property name=\"ThirdProp\"><![CDATA[  ]]></PackageProperty>";
String test1 = "<Property name=\"SecondProp\"><![CDATA[ Some Value ]]></PackageProperty>";
test = test.replaceAll(".*SecondProp.*", test1);
System.out.println(test);

This should work the resulting output for this code is:

<Property name="FirstProp"><![CDATA[  ]]></PackageProperty>
<Property name="SecondProp"><![CDATA[ Some Value ]]></PackageProperty>
<Property name="ThirdProp"><![CDATA[  ]]></PackageProperty>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM