I'm trying to replace one syntax with another and i'm doing this through regex expressions where I would like to replace with one pattern with another.
Pattern pattern = Pattern.compile("length\\((?<field>[a-zA-Z]+)\\)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(statement);
if (matcher.find())
statement = matcher.replaceAll("LEN(" + matcher.group("field") + ")");
return statement;
It's a simple thing where I would like to replace all (loop through) the matches and replace them with another text. however i'm struggling with having the group iterate aswell dynamically.
Expected :select *,LEN(devise) lendevise,LEN(marche) lenmarche,LEN(nature) lennature from tableX where nseq='0'
Actual :select *,LEN(devise) lendevise,LEN(devise) lenmarche,LEN(devise) lennature from tableX where nseq='0'
So as u can notice here. the group value is always replaced by the group of the first match instead of the respective match which is being replaced?
Is there an efficient "best" way of doing this? I would like to avoid (if possible) to put the different groups in an separate arrays.
statement?statement.replaceAll("length\\(([a-zA-Z]+)\\)", "LEN($1)"), see demo.