Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to parse through HTML validation errors by adding the errors to an array, and then looping through the array, strip out the first part of the error (e.g. ValidationError line 23 col 40:) and then I want to only keep the text inside the single quotes and save that to a new list.

Here is what I have working, but I know it's not scalable and only works with the String fullText and not with the ArrayList list so that's what I need help with. Thank you!

package htmlvalidator;

import java.util.ArrayList;

public class ErrorCleanup {

public static void main(String[] args) {
    //Saving the raw errors to an array list
    ArrayList<String> list = new ArrayList<String>();

    //Add the text to the first spot
    list.add("ValidationError line 23 col 40:'Bad value ius-cors for attribute name on element meta: Keyword ius-cors is not registered.'");

    //Show what is in the list
    System.out.println("The full error message is: " + list);       

    String fullText = "ValidationError line 23 col 40:'Bad value ius-cors for attribute name on element meta: Keyword ius-cors is not registered.'";

    //Show just the actual message
    System.out.println("The actual error message is: " + fullText.substring(fullText.indexOf("'") + 1));


}

}
share|improve this question

1 Answer 1

Use a foreach loop:

List<String> list = new ArrayList<String>();
List<String> msgs = new ArrayList<String>();
for (String s : list) {
    msgs.add(s.replaceAll(".*'(.*)'.*", "$1"));
}
list = msgs;

Using regex to extract the string is cleaner and still scalable enough.

share|improve this answer
    
You could stop the substring before last ', it would be nicer ... –  Serge Ballesta Nov 19 at 22:50
    
@serge or use regex! (edited) –  Bohemian Nov 20 at 5:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.