【发布时间】:2017-08-22 14:32:19
【问题描述】:
我阅读了很多关于用正则表达式替换 xml 值中的字符串的文章。在许多文章中,大多数人都接受了
的答案str=str.replaceAll("\\b2017\\b", "****");
但这并没有按预期工作,并且在其他地方也取代了 2017 年。下面是我的例子。
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 who sold policy\"/> "+
" </PaymentCard> "+
" </PaymentForm> "+
" </InsuranceCustomer> "+
" </PlanForBookRQ> "+
"</OTA_InsuranceBookRQ>";
str=str.replaceAll("\\b2017\\b", "****");
System.out.println(str);
}
}
当我运行这个程序时,预期结果是 <PaymentCard ExpireDate="2017"> 应该替换为 <PaymentCard ExpireDate="****"> 但同时它也将 <PurchaseDate>2017-08-21</PurchaseDate> 中的 2017 值替换为 <PurchaseDate>****-08-21</PurchaseDate>,这在我的情况下是不可接受的。
我也尝试了下面的正则表达式,但没有运气。
str=str.replaceAll("(?<!\\S)2017(?!\\S)", "****");
请不要将其标记为重复并关闭它,因为没有答案按预期工作。
【问题讨论】:
-
使用 xml 工具(dom/xpath 等)转换 xml 数据总是更安全。 regex 和 xml 之间的关系很不稳定。特别是考虑到这是信用卡信息,我不会使用黑客。