什么是正则表达式来替换和直到

时间:2018-04-20 10:13:04

标签: java regex

我想替换

clientAddress=meat,London , lXV 546,United Kingdom, bankAddress=25,

clientAddress=maskedAddress, bankAddress=25,

我尝试了从字符串库:

replaceAll("clientAddress=[^\\, bankAddress]*", "clientAddress=maskedAddress, bankAddress"));

然而这不起作用?

3 个答案:

答案 0 :(得分:1)

String address="clientAddress=meat,London , lXV 546,United Kingdom, bankAddress=25,";

address=address.replaceAll("(clientAddress=)(.+)(, bankAddress)", "clientAddress=maskedAddress, bankAddress");
System.out.println(address);

答案 1 :(得分:0)

你能用那个吗?

public static void main(String[] ars) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, IOException, BadPaddingException {


     String test="clientAddress=meat,London , lXV 546,United Kingdom, bankAddress=25,";
     String replaced=test.replaceAll("(.*clientAddress=)(.+)(, bankAddress.+)", "$1maskedADdress$3");
     System.out.println(replaced);

}

答案 2 :(得分:-1)

您可以使用

String s = "clientAddress=meat,London , lXV 546,United Kingdom, bankAddress=25,";
System.out.println(s.replaceAll("(clientAddress=)(.*?)(, bankAddress=.*?)", "$1maskAddress$3"));