1

I have the string Q and i want to separate it into multiple parts. For example i would like to have the (ADD equal 1 and (EXP 1 4) equal 2 and so on splitting up after each parentheses but still maintains the form of the string.

String q =(("(ADD(EXP 1 4)(SQR 1)(DIV 34 77)(MULT 12 5 3 7)(Sub 1 2"));

I've tried doing it like this but haven't had any success.

        s[0]=q.indexOf("(");
        s[1]=q.indexOf(")");
Tarik
  • 4,961
  • 3
  • 36
  • 67
  • `indexOf` returns an integer, so the code you have so far creates an array of integers. – ajb Feb 08 '15 at 02:28
  • Sorry just Java, and i want them to be a set of smaller strings possibly in an array? – french montana Feb 08 '15 at 02:29
  • http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-. Actually, that might help, but it's not really clear what you need. Do you want each result string to start with `(`? Or do you want something more complicated? – ajb Feb 08 '15 at 02:29
  • Use the String.split(); This will allow you to have one or multiple delimiters. Then just go through and assign the correct sub-strings to your array. – Evan Bechtol Feb 08 '15 at 02:30
  • If i split the string and then try to print out the string, will it print out fully? Or will it be fully split from the original? – french montana Feb 08 '15 at 02:35
  • 1
    `Strings` in Java are _immutable_. That means the library doesn't provide any methods to alter the original string. Everything you do will create a new string (or for `split`, an array of strings) but leave the original string alone. Keep that in mind if you ever use `s.replace(...)` or something, because a lot of people incorrectly think they're changing `s` when they do this. – ajb Feb 08 '15 at 02:39

3 Answers3

0

The following:

String q ="(ADD(EXP 1 4)(SQR 1)(DIV 34 77)(MULT 12 5 3 7)(Sub 1 2";
String[] result = q.split("(?=\\()");

produces the following strings in the result array:

"(ADD"
"(EXP 1 4)"
"(SQR 1)"
"(DIV 34 77)"
"(MULT 12 5 3 7)"
"(Sub 1 2"

but I'm not totally sure that's what you want (if it isn't, you'll need to clarify for us).

The argument to split is a regular expression. It's common to use it with something like "/", which will split an input string by slash characters. But doing that doesn't leave the slash characters in the result. To split on a string but leave the delimiters in the results, you need lookahead. The regular expression above does that. (?=<something>) looks ahead for some pattern but doesn't "consume" it; here the pattern is \\( which means a single ( character, but it needs to be preceded by a backslash because ( normally has special meanings in a regular expression.

ajb
  • 31,309
  • 3
  • 58
  • 84
0
q = q.replaceAll(")","");

String[] splittedString = q.split("(");

Now you have array of strings with elements like below.

ADD

EXP 1 4

SQR 1

Hope this is what you expected.

0

If you want all elements in brackets separately, you can use a very simple (and easily readable) regex with the split method:

String[] array = s.split("[(*|)*|(*)*|)*(*]");

This will give you {ADD, EXP 1 4, SQR 1, DIV 34 77, MULT 12 5 3 7, Sub 1 2}.

If you want to keep nesting, that's more complicated and I'd strongly suggest you take a look at regex tutorials such as http://www.regular-expressions.info/tutorial.html.

s.d
  • 4,017
  • 5
  • 35
  • 65