2

I want to string split the following String

String ToSplit = "(2*(sqrt 9))/5";

into the following array of String:

String[] Splitted = {"(", "2", "*", "(", "sqrt", "9", ")", ")", "/", "5"};

As you can see the string ToSplit doesn't have spaces and I am having a hard time splitting the word " sqrt " from the rest of the elements because it is a full word. I am doing:

String[] Splitted = ToSplit.split("");

and the word " sqrt " is splitted into {"s", "q", "r", "t"} (obviously) and I want it splitted as the whole word to get the String splitted as shown above

How can I separate the word " sqrt " (as 1 element) from the others ?

Thanks in advance.

jww
  • 97,681
  • 90
  • 411
  • 885
Arthur
  • 171
  • 6
  • 1
    and then what would you expect if you tried it with `"(25*(sqrt 9))/5";` – Scary Wombat Mar 14 '19 at 04:55
  • 1
    Why do you want to split the string? Smells like an [XY Problem](http://xyproblem.info/). See also [java method for parsing nested expressions](https://stackoverflow.com/questions/7251781/java-method-for-parsing-nested-expressions). – shmosel Mar 14 '19 at 04:58

1 Answers1

1

Here is a working solution which splits on lookarounds. See below the code for an explanation.

String input = "(2*(sqrt 9))/5";
String[] parts = input.split("(?<=[^\\w\\s])(?=\\w)|(?<=\\w)(?=[^\\w\\s])|(?<=[^\\w\\s])(?=[^\\w\\s])|\\s+");
for (String part : parts) {
    System.out.println(part);
}

(
2
*
(
sqrt
9
)
)
/
5

There are four terms in the regex alternation, and here is what each one does:

(?<=[^\\w\\s])(?=\\w)
split if what precedes is neither a word character nor whitespace, AND
what follows is a word character
e.g. split (2 into ( and 2

(?<=\\w)(?=[^\\w\\s])
 split if what precedes is a word character AND
 what follows is neither a word character nor whitespace
e.g. split 9) into 9 and )

(?<=[^\\w\\s])(?=[^\\w\\s])
split between two non word/whitespace characters
e.g. split )/ into ) and /

\\s+
finally, also split and consume any amount of whitespace
as a separator between terms
e.g. sqrt 9 becomes sqrt and 9
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thank you a lot ! You helped me out here 100% ! There is just another issue, running the program I realised the space between the word "sqrt" and the number 9 ("sqrt 9") matters, so, I need aswell to separate by space whereas the spaces appear. What do I have to add do the split ? – Arthur Mar 14 '19 at 05:02
  • 1
    @Rafa I added a detailed explanation of how the answer works. – Tim Biegeleisen Mar 14 '19 at 05:09
  • Thank you a lot again ! This makes me feel dumb and realise there is so much to explore in programming. I would never get my answer alone. Thanks bro, also thanks for the explanation, not that I will remember the code when I need but at least I understand the meaning of the type of splits – Arthur Mar 14 '19 at 05:16