helps in capturing multiple strings and stores them in an array

0
String names = "<td><input type="radio" name="pergunta23g" value="SIM"/> Qual? <input type="text" name="pergunta23gQual" class="dados"> Onde<input type="text" name="pergunta23gOnde" class="dados"></td>"

I would like to save these three values: I would like to get only the names without the quotes in this string. How do I do the treatment? store in a string array like question23g, question23gQual, and ask23git.

    
asked by anonymous 05.06.2015 / 20:19

1 answer

0
String names = "<td><input type=\"radio\" name=\"pergunta23g\" value=\"SIM\"/> Qual? <input type=\"text\" name=\"pergunta23gQual\" class=\"dados\"> Onde<input type=\"text\" name=\"pergunta23gOnde\" class=\"dados\"></td>";
String[] inputs = names.split("input");
Pattern pattern = Pattern.compile("name=\"(.*?)\"");

List<String> valores = new ArrayList();
for (String input : inputs) {
    Matcher matcher = pattern.matcher(input);

    if (matcher.find())
    {
        valores.add(matcher.group(1));
    }
}

for (String valor : valores) {
    System.out.println(valor);
}

Output

  

question23g

     

question23gQual

     

question23 Where

Explanations:

  • I added \ to "
  • I used the Pattern
  • name=\"(.*?)\" will get only what you have inside " after name=
  • I made split in an Array to use matcher in each input
  • I added the correct values to a new list
05.06.2015 / 20:38