I have this string:
"round((TOTAL_QTY * 100) / SUM(ORDER_ITEMS->TOTAL_QTY) , 1)"
I tried to split the string using the following code:
String[] tokens = function.split("[ )(*+-/^!@#%&]");
Result is the following array:
"round"
""
"TOTAL_QTY"
""
""
"100"
""
""
""
"SUM"
"ORDER_ITEMS"
"->TOTAL_QTY"
""
""
""
"1"
But I need to split the string as follows:
"round",
"TOTAL_QTY",
"100",
"SUM",
"ORDER_ITEMS->TOTAL_QTY",
"1"
To make it clearer. First of all I need to ignore ->
when it splits the string and then remove those empty strings in the result array.
To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.
If you want to split a string that matches a regular expression (regex) instead of perfect match, use the split() of the re module. In re. split() , specify the regex pattern in the first parameter and the target character string in the second parameter.
You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.
You do not only have to use literal strings for splitting strings into an array with the split method. You can use regex as breakpoints that match more characters for splitting a string.
Ok, I think you can do it in two steps, replace all non necessary characters with space for example and then split with space, your regex can look like like :
[)(*+/^!@#%&,]|\\b-\\b
Your code :
String[] tokens = function.replaceAll("[)(*+/^!@#%&,]|\\b-\\b", " ").split("\\s+");
Note that I used \\b-\\b
to replace only -
:
Or If you want something clean, you can use Pattern with Matcher like this :
Pattern.compile("\\b\\w+->\\w+\\b|\\b\\w+\\b")
.matcher("round((TOTAL_QTY * 100) / SUM(ORDER_ITEMS->TOTAL_QTY) , 1)")
.results()
.map(MatchResult::group)
.forEach(s -> System.out.println(String.format("\"%s\"", s)));
regex demo
Details
\b\w+->\w+\b
to match that special case of ORDER_ITEMS->TOTAL_QTY
|
or\b\w+\b
any other word with word boundariesNote, this solution work from Java9+, but you can use a simple Pattern and Matcher solution.
Outputs
"round"
"TOTAL_QTY"
"100"
"SUM"
"ORDER_ITEMS->TOTAL_QTY"
"1"
Could see a couple of very good solutions provide by YCF_L
Here is one more solution:
String[] tokens = function.replace(")","").split("\\(+|\\*|/|,");
Explanation:
\\(+
Will split by (
and +
will ensure that multiple open bracket cases and handled e.g. round((
|\\*|/|,
OR split by *
OR split by /
OR split by ,
Output:
round
TOTAL_QTY
100
SUM
ORDER_ITEMS->TOTAL_QTY
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With