N3lson Posted February 26, 2018 at 07:39 PM Report #609513 Posted February 26, 2018 at 07:39 PM (edited) Boas eu sou iniciante no java, e estou perante um problema que é o seguinte. Pretendo transformar uma String (query sql ) dependendo dos valores de entrada. Até ao momento consegui fazer o seguinte String str = "SELECT t_nome FROM tprom001, tremi450 WHERE t_func=252"; Pattern p = Pattern.compile("t\\D{4}\\d{3}"); Matcher m = p.matcher(str); while (m.find()) { //System.out.println("t" + m.group() + "100"); str = str.replaceAll("t\\D{4}\\d{3}", "t" + m.group() + "100"); } O que pretendia era transformar a string para ficar deste modo SELECT t_nome FROM ttprom001100, ttremi450100 where t_func=252 mantendo o valor a sublinhado e acrescentado os valores a negrito, mas o que esta a acontecer neste momento é que adiciona 2x o t e o 100 Edited February 26, 2018 at 08:10 PM by N3lson You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
N3lson Posted February 26, 2018 at 07:49 PM Author Report #609515 Posted February 26, 2018 at 07:49 PM (edited) Coloquei desta forma, apesar de corrigir e ficar numa string mas repete o mesmo campo. String str = "SELECT t_nome FROM tprom001, tremi450 WHERE t_func=252"; Pattern p = Pattern.compile("t\\D{4}\\d{3}"); Matcher m = p.matcher(str); String x = null; while (m.find()) { //System.out.println("t" + m.group() + "100"); //str = str.replaceAll("t\\D{4}\\d{3}", "t" + m.group() + "100"); //System.out.println(str.replaceAll("t\\D{4}\\d{3}", "t" + m.group() + "100")); x = str.replaceAll("t\\D{4}\\d{3}", "t" + m.group() + "100"); } System.out.println(x); Edited February 26, 2018 at 08:10 PM by N3lson Update : Erro da string, não reparei adiciona o mesmo campo You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
Solution N3lson Posted February 27, 2018 at 09:37 AM Author Solution Report #609522 Posted February 27, 2018 at 09:37 AM Resolvido, estava a dormir, alteração dentro do while x = str.replaceAll(mgroup(), "t" + m.group() + "100"); You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now