0

I'm aware that to split by brackets I need to use the \\[ notation, per this thread: Split using a bracket

However, I need to get tokens from both [ and ].

An example string is study[2] from whcih I need to get the token 2.

The following gives an Error in Eclipse's Expression Debugger:

"study[2]".split("\\[\\]");

Is there another regexp I should use?

Community
  • 1
  • 1
gene b.
  • 10,512
  • 21
  • 115
  • 227
  • 1
    Possible duplicate of [How to extract a substring using regex](http://stackoverflow.com/questions/4662215/how-to-extract-a-substring-using-regex). It looks like you're really trying to just get the substring between the two brackets. That question is similarly asking about how to get the substring between two single quotes. – Thunderforge Mar 14 '17 at 19:08
  • 2
    You say you want to *split*, but it sounds more like you want to extract a substring defined by the pattern. If so, then `String.split()` is the wrong tool for the job. – John Bollinger Mar 14 '17 at 19:09

3 Answers3

2

If you really want to split, you have to do a group of the brackets.

"study[2]".split("[\\[\\]]");

Because that String is difficult to read, here again the regex without Java-escaping

[\[\]]

This means, you have a group (the outer brackets) containing two elements, [ and ], each of which is escaped. This means, split at either [ or ]. According to this, a String like "a]bc[1]2[][3" will be split at every bracket, so you get a, bc, 1, 2, 3.

If your problem is simpler than I assume here, the answer of @user1766169 is less of an overkill.

SilverNak
  • 3,283
  • 4
  • 28
  • 44
  • Well basically my question calls for a StringTokenizer, where I can do "[]" without a regexp. But I hate doing nextToken() twice. – gene b. Mar 14 '17 at 19:36
  • thanks for the response I guess that was it. was missing that extra ] – gene b. Mar 14 '17 at 19:48
2

If you need to split using either of [ or ], then use

"study[2]".split("[\\[\\]]");

Output:-

study
2

If your intention is to extract the number within the brackets, use below:-

Pattern p = Pattern.compile(".*\\[([0-9]+)\\].*");
Matcher m = p.matcher("study[2]");
System.out.println(m.matches());
System.out.println(m.group(1));

OutPut:-

true
2

Note:- the m.matches() needs to be called, otherwise m.group(1) will not return any result.

Regex String in Detail

.*  - match any character 0 or more times 

\\[  - an escaped [ character

(  - open the braces to group items. These matched groups can be later re-used.

[0-9]+  - match any number.

)  - grouping ends. Anything inside a group is accessible by calling group(1) (1 for the first group). 

\\]  - an escaped ] character

.*
Rimmy Mathew
  • 157
  • 7
1

Use substring instead:

String testString = "study[2]";
int startIndex = testString.indexOf("[");
int endIndex = testString.indexOf("]");
String subString = testString.substring(startIndex+1, endIndex);
System.out.println(subString);

This will print 2.

user1766169
  • 1,932
  • 3
  • 22
  • 44
  • 1
    This will cause an issue for a string like `Apple ][` where brackets are in different orders, and will also only find the first match if there are multiples as in `foo[1]bar[2]`. But if all input strings are guaranteed to be formatted like `foo[1]`, then this really is the best solution since regexes would be overkill. – Thunderforge Mar 14 '17 at 19:19