Answer without using Regex:
import java.util.*;
class Program {
public static void main (String[] args) {
String texto = "(:TEXTOQUALQUER NADA DO FOI :TEXTOQQDENOVO SERÁ DE NOVO :TEXTOQQMAIS DO JEITO QUE UM DIA :TEXTO3343)";
List<String> textos = new ArrayList<String>();
while (texto.length() > 0) {
texto = texto.substring(texto.indexOf(":") + 1);
int posicaoParentese = texto.indexOf(")");
int posicaoEspaco = texto.indexOf(" ");
int posicaoFinal = Math.min((posicaoParentese == -1 ? Integer.MAX_VALUE : posicaoParentese), (posicaoEspaco == -1 ? Integer.MAX_VALUE : posicaoEspaco));
textos.add(texto.substring(0, posicaoFinal));
texto = texto.substring(posicaoFinal + 1);
}
for (String item : textos) System.out.println(item);
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I’m leaving the previous attempts to help anyone with a similar problem. The question was quite confusing forcing the answers (not only mine) to be edited to get the desired result. I hope it is now ok.
Reading your question better I think you want something else, I think it would just be istol
import java.util.*;
class Program {
public static void main (String[] args) {
String texto = "(:TEXTOQUALQUER NADA DO FOI :TEXTOQQDENOVO SERÁ DE NOVO :TEXTOQQMAIS DO JEITO QUE UM DIA :TEXTO3343)";
List<String> textos = new ArrayList<String>();
while (texto.length() > 0) {
texto = texto.substring(texto.indexOf(":") + 1);
int posicaoParentese = texto.indexOf(")");
int posicaoEspaco = texto.indexOf(" ");
int posicaoFinal = Math.min((posicaoParentese == -1 ? Integer.MAX_VALUE : posicaoParentese), (posicaoEspaco == -1 ? Integer.MAX_VALUE : posicaoEspaco));
textos.add(texto.substring(0, posicaoFinal));
texto = texto.substring(posicaoFinal + 1);
}
for (String item : textos) System.out.println(item);
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
If it has not yet been answered, you do not need Regex for this, just a Split()
:
class Program {
public static void main (String[] args) {
String texto = "(:TEXTOQUALQUER NADA DO FOI :TEXTOQQDENOVO SERÁ DE NOVO :TEXTOQQMAIS DO JEITO QUE UM DIA :TEXTO3343)";
String[] textos = texto.split(":");
for (String item : textos) System.out.println(item);
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
If you don’t want what comes before the first :
simply ignore element 0 of arryay (texts[0]).
Suggestion: https://regex101.com/r/dM1vW1/2
– Sergio
Usually Regex for this kind of thing is an exaggeration. Incidentally, Regex is that thing that should only be used when the normal possibilities of solving the problem have really been exhausted.
– Bacco
@Bacco I agree that it is exaggeration, however it is the caveat that depending on the case its use may be the most robust way to treat the input. After all, what is text? Whichever character other than
:
and)
? Or is there more involved? In the impossibility of making one parse complete (which can be even more Overkill that use regex) this technique helps to isolate small simple fragments within a complex structure - without having to interpret this whole structure.– mgibsonbr
@mgibsonbr In this case I can’t imagine any reason for Regex, honestly. If you are dealing with 2 simple and static delimiters, just go forward the next pro pointer (thing the regex will have to do, qq shape). Although when it comes to java, you may even be able to make regex more efficient, given the "consistency" with which each thing is implemented internally... PS: But I find perfectly valid answers given the will of the OP, by the way.
– Bacco