2

I have code that reading a file contains multiple square brackets [] in one line, i will take that value (inside square brackets) and will be replaced by another string. The problem is i just got first square brackets value in the line and the others cannot be handled. This is my code :

if (line.contains("[") && line.contains("]")) {
    getindex = getIndexContent(line);

}

And the method to get the index value:

String getIndexContent(String str) {
    int startIdx = str.indexOf("[");
    int endIdx = str.indexOf("]");

    String content = str.substring(startIdx + 1, endIdx);
    return content;
}

And this is the file contain square brackets that i read:

 var[_ii][_ee] = init_value;

Well, i have got the _ii value but how get the _ee that the second value of square brackets? I just imagine that store in Array, but i don't know how?

Thanks.

Samsul Arifin
  • 247
  • 1
  • 6
  • 24

3 Answers3

3

you can iterate through your String until you got all also make life easy by returning all within one method:

List<String> getIndexContent(String str) {

 List<String> list = new ArrayList<String>();

 while(true){
    if(!str.contains("[") && !str.contains("]")){
      break;
    }
    int startIdx = str.indexOf("[");
    int endIdx = str.indexOf("]");

    String content = str.substring(startIdx + 1, endIdx);
    list.add(content);
    if(endIdx==str.length()-1){
      break;
    }
    str=str.subString(endIdx+1,str.length());
 }

    return list;
}

NOTE:

it won't work on nested brackets

nafas
  • 5,283
  • 3
  • 29
  • 57
  • Thanks for your comment. I have tried this code. But it seem different data type, i mean must convert from String to list. Nice code! – Samsul Arifin Nov 19 '15 at 17:49
  • @SamsulArifin well, if you want to extract all matched information then this is one way of doing it, since you may have more than one then you need to store the data in a different datatype... – nafas Nov 20 '15 at 09:11
1

indexOf takes an optional positional argument for the starting point of your search. If you set that to your end index, endIdx, plus one, it will find the second occurrence of the brackets.

int startIdx2 = str.indexOf("[", endIdx + 1);
int endIdx2 = str.indexOf("]", endIdx + 1);
Sam McCreery
  • 542
  • 3
  • 18
1

You can also do it with regex like this.

        Pattern pattern = Pattern.compile("\\[[^\\[.]+?\\]");

        String str = "dt = (double[]) obj[i];";

        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }

You can also get the first and last indices of every matches. matcher.start() and matcher.end() will return the starting index and the ending index of the current match.

sohan nohemy
  • 615
  • 5
  • 13
  • @Samul Afifin No, It does not. It's just need to be recursive for that – sohan nohemy Nov 28 '15 at 07:09
  • Then how if line contains nested square brackets, any solution? – Samsul Arifin Nov 28 '15 at 07:10
  • Change Pattern.compile("\\[.+?\\]") to Pattern.compile("\\[[^\\[.]+?\\]") – sohan nohemy Nov 28 '15 at 07:36
  • i have problem with Pattern.compile("\\[.+?\\]") as you answered above. why pattern cannot found this [i] in this line code 'dt = (double[]) obj[i];' ? What's wrong? – Samsul Arifin Dec 10 '15 at 12:57
  • @Samsul Arifin I have edit my answer. Please check again. – sohan nohemy Dec 10 '15 at 16:45
  • 1
    thanks. I have edited your answer before and replacing + with *, everything fine. Would you mind, if i ask you about another topic. I have a problem with how to read a method and variable insides. I mean i read a java class that contains some method. then how i can get methods and variable inside in the class? please check this link (http://stackoverflow.com/questions/34083519/check-duplicate-variable-in-a-method-scope-java/) – Samsul Arifin Dec 10 '15 at 16:59