57

Possible Duplicate:
substring between two delimiters

I have a string like

"ABC[ This is to extract ]"

I want to extract the part "This is to extract" in java. I am trying to use split, but it is not working the way I want. Does anyone have suggestion?

Community
  • 1
  • 1
yogsma
  • 10,142
  • 31
  • 97
  • 154
  • 1
    try using [regular expressions](http://docs.oracle.com/javase/tutorial/essential/regex/) – vainolo Dec 10 '12 at 07:07
  • 4
    What have you tried? It's hard to know why what you're doing isn't working without seeing your code. – Jon Skeet Dec 10 '12 at 07:07
  • use StringTokenizer("delimeter","String") – curious Dec 10 '12 at 07:15
  • 1
    String bar = StringUtils.substringBetween("ABC[ This is to extract ]", "[", "]"); please see the documentation : https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html – Haid Sep 20 '18 at 21:56

4 Answers4

104

If you have just a pair of brackets ( [] ) in your string, you can use indexOf():

String str = "ABC[ This is the text to be extracted ]";    
String result = str.substring(str.indexOf("[") + 1, str.indexOf("]"));
Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • 3
    Functions `indexOf` and `substring` internally iterates over characters. So keep in mind that when you call `srt.substring(str.indexOf, str.indexOf)` original `str` is iterated 3 times. Which may lead to performance problem in large strings. – Cherry Feb 11 '15 at 11:13
  • 1
    indexOf('[') might be a bit faster than indexOf("[") – pi3 Apr 19 '16 at 10:09
72

If there is only 1 occurrence, the answer of ivanovic is the best way I guess. But if there are many occurrences, you should use regexp:

\[(.*?)\] this is your pattern. And in each group(1) will get you your string.

Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(input);
while(m.find())
{
    m.group(1); //is your string. do what you want
}
Roland
  • 1,220
  • 1
  • 14
  • 29
shift66
  • 11,760
  • 13
  • 50
  • 83
  • This is what I am trying Pattern somePart = Pattern.compile("\\b[.*?\\b]"); Matcher matcher = somePart.matcher(info); – yogsma Dec 10 '12 at 07:16
  • 1
    See answer to this thread for applying above method http://stackoverflow.com/questions/4662215/java-how-to-extract-a-substring-using-regex – Mudassir Hasan Dec 10 '12 at 07:17
9

Try as

String s = "ABC[ This is to extract ]";
        Pattern p = Pattern.compile(".*\\[ *(.*) *\\].*");
        Matcher m = p.matcher(s);
        m.find();
        String text = m.group(1);
        System.out.println(text);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
8
  String s = "ABC[This is to extract]";

    System.out.println(s);
    int startIndex = s.indexOf('[');
    System.out.println("indexOf([) = " + startIndex);
    int endIndex = s.indexOf(']');
    System.out.println("indexOf(]) = " + endIndex);
    System.out.println(s.substring(startIndex + 1, endIndex));
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
MilanPanchal
  • 2,943
  • 1
  • 19
  • 37