helps in capturing multiple strings and storing them in an array

Asked

Viewed 251 times

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 keep these three values: I’d like to take only the Noames without the quotation marks on that string. How do I do the treatment? store in a string array as question23g, question23gQual, and question23gOwhere.

  • Can you [Edit] the question and explain it better? This string comes from where? There are better ways to make a string to then reuse. JSON would be better. If you cannot change the string structure you will need regex for example and then it is useful if you explain the pattern that the string has: are the fields always the same? names are always one word? etc

  • corrected, it follows this pattern, but may vary.

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);
}

Exit

question23g

question23gQual

question23gOwhere

Explanations:

  • I added the \ us "
  • I used the class Pattern
  • name=\"(.*?)\" will only take what is inside the " after the name=
  • I split into an Array to use the matcher in each input
  • I added the correct values to a new list

Browser other questions tagged

You are not signed in. Login or sign up in order to post.