Prakash
Prakash

Reputation: 693

how to split a string by "|"

I want to use regular expression to split this string:

String filter = "(go|add)addition|(sub)subtraction|(mul|into)multiplication|adding(add|go)values|(add|go)(go)(into)multiplication|";

I want to split it by | except when the pipe appears within brackets in which case they should be ignored, i.e. I am excepting an output like this:

(go|add)addition
(sub)subtraction
(mul|into)multiplication
adding(add|go)values
(add|go)(go)(into)multiplication

Updated

And then i want to move the words within the brackets at the start to the end.

Something like this..

addition(go|add)
subtraction(sub)
multiplication(mul|into)
adding(add|go)values
multiplication(add|go)(go)(into)

I have tried this regular expression: Splitting of string for `whitespace` & `and` but they have used quotes and I have not been able to make it work for brackets.

Upvotes: 1

Views: 148

Answers (4)

Robin
Robin

Reputation: 9644

If you don't have nested parenthesis (so not (mul(iple|y)|foo)) you can use:

((?:\([^)]*\))*)([^()|]+(?:\([^)]*\)[^()|]*)*)

(                          #start first capturing group
    (?:                    # non capturing group
         \([^)]*\)         # opening bracket, then anything except closing bracket, closing bracket
    )*                     # possibly multiple bracket groups at the beginning
)
(                          # start second capturing group
    [^()|]+                # go to the next bracket group, or the closing |
    (?:
        \([^)]*\)[^()|]*   # bracket group, then go to the next bracket group/closing |
     )*                    # possibly multiple brackets groups
)                          # close second capturing group

and replace with

\2\1

Explanation

  • ((?:\([^)]*\))*) matches and captures all the parenthesis groups at the beginning
  • [^()|]* anything except (, ), or |. If there isn't any parenthesis, this will match everything.
  • (?:\([^)]*\)[^()|]*): (?:...) is a non capturing group, \([^)]*\) matches everything inside parenthesis, [^()|]* gets us up to the next parenthesis group or the | that ends the match.

Code sample:

String testString = "(go|add)addition|(sub)subtraction|(mul|into)multiplication|adding(add|go)values|(add|go)(go)(into)multiplication|";
Pattern p = Pattern.compile("((?:\\([^)]*\\))*)([^()|]+(?:\\([^)]*\\)[^()|]*)*)");
Matcher m = p.matcher(testString);
while (m.find()) {
    System.out.println(m.group(2)+m.group(1));
}

Outputs (demo):

addition(go|add)
subtraction(sub)
multiplication(mul|into)
adding(add|go)values
multiplication(add|go)(go)(into)

Upvotes: 2

CrimOudin
CrimOudin

Reputation: 5

Set up bool to keep track if you are inside a parenthesis or not.

Bool isInside = True;

loop through string
if char at i = ")" isInside = False
if isInside = false
code for skipping |
else
code for leaving | here

something like this should work i think.

Upvotes: 0

santosh kumar
santosh kumar

Reputation: 21

Your String

"(go|add)addition|(sub)subtraction|(mul|into)multiplication|"

have a pattern |( from where you can split for this particular String pattern. But this wont give expected result if your sub string contains paranthesis( "(" ) in between ex:

(go|(add))addition.... continue

Hope this would help.

Upvotes: 0

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31283

Already seen this question 15 min ago. Now that it is asked correctly, here is my proposition of answer :

Trying with a regex is complex because you need to count parenthesis. I advice you to manually parse the string like this :

public static void main(String[] args) {

  String filter = "(go|add)addition|(sub)subtraction|(mul|into)multiplication|";

  List<String> strings = new LinkedList<>();
  int countParenthesis = 0;
  StringBuilder current = new StringBuilder();

  for(char c : filter.toCharArray()) {
    if(c == '(') {countParenthesis ++;}
    if(c == ')') {countParenthesis --;}
    if(c == '|' && countParenthesis == 0) {
      strings.add(current.toString());
      current = new StringBuilder();
    } else {
      current.append(c);
    }
  }
  strings.add(current.toString());

  for (String string : strings) {
    System.out.println(string+" ");
  }

}

Output :

(go|add)addition 
(sub)subtraction 
(mul|into)multiplication 

Upvotes: 3

Related Questions