-3

I have the following code segment:

function test(){
   try {
   ---------------some contents-------
       }
   catch(e){
           }
}

Now, I want the codes between the 1st pair of curly braces. The output should be like:

   try {
   ---------------some contents-------
       }
   catch(e){
           }

How can I do that with or without using Regex? I tried using the following regex:

Pattern p = Pattern.compile("\\{([^}]*)\\}");
        Matcher m = p.matcher(s); // s contains each line of the above text
        while (m.find()) {
        System.out.println(m.group(1));
        }

But, It only fetches contents if its there in a single line or no multiple lines of braces exist.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Yes. But I didn't get any suitable solution – New_Coder_82 Sep 23 '15 at 10:50
  • Can you show us what you have tried so far? – R Day Sep 23 '15 at 10:51
  • is this supposed to be Java, or Javascript? and really: you googled and tried some regex but you could not find a sollution for this? – Stultuske Sep 23 '15 at 10:51
  • 1
    Pattern p = Pattern.compile("\\{([^}]*)\\}"); Matcher m = p.matcher(s); while (m.find()) { System.out.println(m.group(1)); } I tried using the above code segment. – New_Coder_82 Sep 23 '15 at 10:53
  • In your regex you exclude the closing brace in your character class (`[^}]*`). Since you want to match everything between the first opening and the last closing brace just use `.*` instead. _But_, since your test text seems to be code, I assume you want to match arbitrary pairs of braces. In that case use a parser and not a regex, since regex doesn't fit code that well. – Thomas Sep 23 '15 at 10:58
  • `System.out.println(s.substring(s.indexOf('{')+1, s.lastIndexOf('}')));` – Rustam Sep 23 '15 at 11:02
  • `It only fetches contents if its there in a single line` - use the `MULTILINE` on the `Pattern` instance or the prefix `(?m)` on your regex to solve this. – Thomas Sep 23 '15 at 11:02
  • Thank you :) I got the problem solved with all of your inputs. – New_Coder_82 Sep 23 '15 at 11:28

2 Answers2

1

You could've searched better

Use substr and indexOf / lastIndexOf:

function test(){
   try      {   /*---------------some contents-------*/    }
   catch(e) {}
}

var testStr = String(test);
testStr = testStr.substr(testStr.indexOf('{') + 1);
document.querySelector('#result').textContent =
   testStr.substr(0, testStr.lastIndexOf('}'));
<pre id="result"></pre>
Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

Just Try It:

String s = "function test(){"
        + "try {"
        + "---------------some contents-------"
        + "}"
        + "catch(e){"
        + "}"
        + "}";
int bracketStartIndex = s.indexOf("{");
System.out.println("START INDEX = " + bracketStartIndex);
int bracketEndIndex = s.lastIndexOf("}");
System.out.println("END INDEX = " + bracketEndIndex);
System.out.println("OUTPUT STRING : " + s.substring(bracketStartIndex, bracketEndIndex));
Bhuwan Prasad Upadhyay
  • 2,916
  • 1
  • 29
  • 33