3

I'm working on a piece of code where I've to split a string into individual parts. The basic logic flow of my code is, the numbers below on the LHS, i.e 1, 2 and 3 are ids of an object. Once I split them, I'd use these ids, get the respective value and replace the ids in the below String with its respective values. The string that I have is as follow -

String str = "(1+2+3)>100";

I've used the following code for splitting the string -

String[] arraySplit = str.split("\\>|\\<|\\=");
String[] finalArray = arraySplit[0].split("\\(|\\)|\\+|\\-|\\*");

Now the arrays that I get are as such -

arraySplit = [(1+2+3), >100];
finalArray = [, 1, 2, 3];

So, after the string is split, I'd replace the string with the values, i.e the string would now be, (20+45+50)>100 where 20, 45 and 50 are the respective values. (this string would then be used in SpEL to evaluate the formula)

I'm almost there, just that I'm getting an empty element at the first position. Is there a way to not get the empty element in the second array, i.e finalArray? Doing some research on this, I'm guessing it is splitting the string (1+2+3) and taking an empty element as a part of the string. If this is the thing, then is there any other method apart from String.split() that would give me the same result?

Edit -

Here, (1+2+3)>100 is just an example. The round braces are part of a formula, and the string could also be as ((1+2+3)*(5-2))>100.

Edit 2 -

After splitting this String and doing some code over it, I'm goind to use this string in SpEL. So if there's a better solution by directly using SpEL then also it would be great.

Also, currently I'm using the syntax of the formula as such - (1+2+3) * 4>100 but if there's a way out by changing the formula syntax a bit then that would also be helpful, e.g replacing the formula by - ({#1}+{#2}+{#3}) * {#4}>100, in this case I'd get the variable using {# as the variable and get the numbers.

I hope this part is clear.

Edit 3 -

Just in case, SpEL is also there in my project although I don't have much idea on it, so if there's a better solution using SpEL then its more than welcome. The basic logic of the question is written at the starting of the question in bold.

HardikT
  • 735
  • 1
  • 8
  • 24
  • @DavidArenburg yeah, I'm using Java – HardikT May 01 '17 at 09:58
  • Show a complex example along with what you expect to get as output. – Tim Biegeleisen May 01 '17 at 11:19
  • @TimBiegeleisen hey, I've updated the question with my code logic and an example. Kindly have a look. – HardikT May 01 '17 at 12:37
  • You still haven't told us what you are trying to do here. If you want to parse a nested algebraic expression, regex is probably a poor choice. Instead, you'd likely want a parser of some kind. – Tim Biegeleisen May 01 '17 at 12:50
  • @TimBiegeleisen here's a brief idea - I'd be having a string containing a formula that would be having some IDs in the LHS *(1,2,3 in the upper case)*. This string is then split to get an array of the IDs. Later, these IDs are replaced in the string with the respective readings *(30,45,50 in the upper case)*. This replaced string is then used in SpEL to get the value of the formula. So the string needs to be tranformed from *(1+2+3)>100* to *(30.0+45.0+50.0)>100*. Hope this clears up some confusion. – HardikT May 01 '17 at 12:54

2 Answers2

2

If you take a look at the split(String regex, int limit)(emphasis is mine):

When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array.

Thus, you can specify 0 as limit param:

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • hey, @thomas this might be helpful in my case and I'll give it a try but this solves half of my problem. I still have an issue while replacing the string with the readings. Kindly have a look at the edited question. – HardikT May 01 '17 at 12:57
  • @Trivedi the original question is *String.split() returns an array with an additional empty value*. If you encounter issue while replacing thoses values we'll need to take a look at the code you wrote and that is not in the scope of this question: you should ask another one :) – Thomas Ayoub May 01 '17 at 13:02
  • Well then could you help me out in the second part of the question, I'll edit the title of the question. Asking a new question won't be a good idea as both of these issues have a same work case and code behind it. Hope you understand my viewpoint here. :) – HardikT May 01 '17 at 13:05
  • @Trivedi please read [One post with multiple questions or multiple posts?](https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts). I'll be glad to help you with another question – Thomas Ayoub May 01 '17 at 13:20
  • hey here's the link to the separate question - http://stackoverflow.com/questions/43720022/string-replace-returns-unwanted-string – HardikT May 01 '17 at 13:45
1

If you keep things really simple, you may be able to get away with using a combination of regular expressions and string operations like split and replace.

However, it looks to me like you'd be better off writing a simple parser using ANTLR.

Take a look at Parsing an arithmetic expression and building a tree from it in Java and https://theantlrguy.atlassian.net/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3

Edit: I haven't used ANTLR in a while - it's now up to version 4, and there may be some significant differences, so make sure that you check the documentation for that version.

Community
  • 1
  • 1
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67