Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using String.split() How can I split a string based on a regular expression excluding a certain string

Tags:

java

regex

split

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.

like image 376
a.toraby Avatar asked Jan 19 '20 08:01

a.toraby


People also ask

How do I split a string in Word with regular expressions?

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.

How do you split a string in regular expression in Python?

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.

How do you split strings based on?

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.

Can we use regex in split a string?

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.


2 Answers

Solution 1

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 - :

Solution 2

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 boundaries

Note, 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"
like image 156
YCF_L Avatar answered Nov 01 '22 16:11

YCF_L


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
like image 36
Govinda Sakhare Avatar answered Nov 01 '22 15:11

Govinda Sakhare