How can I split string by a special character and ignore everything inside parentheses?

80
February 12, 2019, at 10:30 PM

I want to split the string by "/" and ignore "/" inside the outer parentheses.

Sample input string:

"Apple 001/(Orange (002/003) ABC)/Mango 003 )/( ASDJ/(Watermelon )004)/Apple 002 ASND/(Mango)"

Expected output in string array:

["Apple 001", "(Orange (002/003) ABC)", "Mango 003 )/( ASDJ", "(Watermelon )004)", "Apple 002 ASND", "(Mango)"]

This is my regex:

\/(?=(?:[^\(\)]*\([^\(\)]*\))*[^\(\)]*$)

But it can only support simple string like this:

"Apple 001/(Orange 002/003 ABC)/Mango 003 ASDJ/(Watermelon 004)/Apple 002 ASND/(Mango)"

If there is inner parentheses, the result is incorrect.

Answer 1

Here's a sample of a parser that would implement your need :

public static List<String> splitter(String input) {
    int nestingLevel=0;
    StringBuilder currentToken=new StringBuilder();
    List<String> result = new ArrayList<>();
    for (char c: input.toCharArray()) {
        if (nestingLevel==0 && c == '/') { // the character is a separator !
            result.add(currentToken.toString());
            currentToken=new StringBuilder();
        } else {
            if (c == '(') { nestingLevel++; }
            else if (c == ')' && nestingLevel > 0) { nestingLevel--; }
            currentToken.append(c);
        }
    }
    result.add(currentToken.toString());
    return result;
}

You can try it here.

Note that it doesn't lead to the expected output you posted, but I'm not sure what algorithm you were following to obtain such result. In particular I've made sure there's no "negative nesting level", so for starters the / in "Mango 003 )/( ASDJ" is considered outside of parenthesis and is parsed as a separator.

Anyway I'm sure you can tweak my answer much more easily than you would a regex answer, the whole point of my answer being to show that writing a parser to handle such problems is often more realistic than to bother trying to craft a regex.

READ ALSO
Comparing data in hql and return boolean value [on hold]

Comparing data in hql and return boolean value [on hold]

I want to compare data in hql and get boolean values to username variable, below some of my codes

81
Bad request only on safari browser

Bad request only on safari browser

I have a problem that I do not understand, on this site, in https and safari I have a bad request, on other browsers it's ok

62
After submitting form, if form_validation false, then redirect user to view and show form&#39;s filled input values using redirect()

After submitting form, if form_validation false, then redirect user to view and show form's filled input values using redirect()

In CodeIgniter, after submitting form, when we get POST data in controller function of form action, I am checking for form_validationBut if form_validation checking returns false, I want to redirect the user to view and show him the form's filled values

78
Better way of importing libraries into a class

Better way of importing libraries into a class

I have developed an external module within an existing applicationI want to make this module more or less reusable

57