Replace exact matching string in xml string using java -
i read many articles regex replace string in xml value. in many articles of peoples accepted answers
str=str.replaceall("\\b2017\\b", "****");
but not working expected , replacing 2017 in other place also. below example.
public class stringtest { public static void main(string[] args) { string str= "<ota_insurancebookrq xmlns=\"http://www.opentravel.org/ota/2003/05\" version=\"2.001\"> "+ " <pos> "+ " <source> "+ " <tpa_extensions> "+ " <productcode>101468</productcode> "+ " <purchasedate>2017-08-21</purchasedate> "+ " <transactiontype>purchase</transactiontype> "+ " <submissiontype>merchantxmlpurchase</submissiontype> "+ " </tpa_extensions> "+ " </source> "+ " </pos> "+ " <planforbookrq planid=\"245235\"> "+ " <inscoveragedetail> "+ " <coveredtrips> "+ " <coveredtrip depositdate=\"2017-08-11t00:00:00.000z\" end=\"2017-09-03\" finalpaydate=\"2017-08-14t00:00:00.000z\" start=\"2017-09-02\"> "+ " <destinations> "+ " <destination> "+ " <stateprov/> "+ " <countryname>germany</countryname> "+ " </destination> "+ " </destinations> "+ " <operators> "+ " <operator companyshortname=\"delta\" travelsector=\"airline\"/> "+ " <operator companyshortname=\"carnival\" travelsector=\"cruiseline\"/> "+ " </operators> "+ " </coveredtrip> "+ " </coveredtrips> "+ " </inscoveragedetail> "+ " <insurancecustomer> "+ " <paymentform costcenterid=\"online\" guaranteeid=\"243356\" rph=\"\" remark=\"customerconfirmation@email.com\"> "+ " <paymentcard expiredate=\"2017\"> "+ " <cardtype code=\"visa\"/> "+ " <cardholdername>test booking</cardholdername> "+ " <telephone phonenumber=\"1234567890\"/> "+ " <email>errorreporting@email.com</email> "+ " <cardnumber encryptedvalue=\"4111111111111111\"/> "+ " <seriescode encryptedvalue=\"agent sold policy\"/> "+ " </paymentcard> "+ " </paymentform> "+ " </insurancecustomer> "+ " </planforbookrq> "+ "</ota_insurancebookrq>"; str=str.replaceall("\\b2017\\b", "****"); system.out.println(str); } }
when ran program expected result <paymentcard expiredate="2017">
should replace <paymentcard expiredate="****">
along replacing 2017 value in <purchasedate>2017-08-21</purchasedate>
<purchasedate>****-08-21</purchasedate>
not acceptable in case.
i tried below regex no luck.
str=str.replaceall("(?<!\\s)2017(?!\\s)", "****");
please not mark , close duplicate since no answers working expected.
if you're trying replace "2017" in quotes, explicitly:
str = str.replace("\"2017\"", "\"****\"");
wiki
Comments
Post a Comment