String sql = "where 1=1 and $P{name} and 1=1 and $P{safa_fs232}"; Pattern p = Pattern.compile("(\\$(P|P!)\\{.*?\\})"); Matcher m = p.matcher(sql); ArrayList<String> strs = new ArrayList<String>(); while (m.find()) { strs.add(m.group(1)); } for (String s : strs){ sql=sql.replace(s, "1=1"); } System.out.println(sql);