javaandroidregexstring

4 substrings from String


I have the following string:

"Sample text, another sample","勾","","勾"

I want to get 4 substrings as follows:

Can someone post solution on how to achieve that? If possible, could you show me two ways to do that (standard way and by using regular expressions). Thanks a lot


Solution

  • String str = "\"Sample text, another sample\",\"勾\",\"\",\"勾\"";
    Pattern pattern = Pattern.compile("\"([^\"]*)\"");
    Matcher matcher = pattern.matcher(str);
    while(matcher.find())
    {
        System.out.println(matcher.group(1));
    }