-2

I want to split a string with space-character but ignoring the space between two brackets.
The code i used is:

String s = "hello (split this) string";
String reg = "(?<=([^\\(].*))( )(?=(.*[^\\)]))";
System.out.println(Arrays.toString(s.split(reg));

My expected output is:

[hello , (split this) , string]  

but i get this error

Exception in thread "main" java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 12 (?<=([^(].))z(?=(.*[^)])) *

I need a regex to get the expected output.
So, somebody please help.

Logesh-0304
  • 109
  • 9
  • Does [**this**](https://regex101.com/r/TzhQ2h/1) help?[Code](https://onlinegdb.com/Skjoo-xh8) –  May 30 '20 at 16:37
  • I run your code and I don't get an error. – Themelis May 30 '20 at 16:52
  • Error will occur in java because op is using variable length quantifier in lookbehinds. Please read [this](https://stackoverflow.com/questions/34616478/positive-lookbehind-regex-obvious-maximum-length) Although there are [some engines](https://stackoverflow.com/a/61831998/7571182) which allow variable length qunatifiers inside lookbehinds. –  May 30 '20 at 16:55
  • 1
    is possseble nesters `((()))` ? –  May 30 '20 at 17:41
  • Replace the comma with `\s+` in the linked solution. – Wiktor Stribiżew May 30 '20 at 18:17
  • @WiktorStribiżew Why would you mark this a duplicate when the answer there does not check if a certain character is BETWEEN a pair of other characters ? Essentially you're saying when a question is posed twice all others should be closed and linked to that one even though it doesn't answer it correctly and is wrong ? –  May 30 '20 at 18:50

2 Answers2

1

You can use the below regex to achieve your requirement:

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

Explanation of the above regex:

[ ] - Represents a space character.

(?=[^\)]*?(?:\(|$)) - Represents a positive look-ahead asserting everything inside of ().

(?:) - Represents a non-capturing group.

| - Represents alternation.

$ - Represents the end of the test String.

You can find the demo of the above regex in here.

Pictorial representation

IMPLEMENTATION IN JAVA

import java.util.Arrays;
public class Main
{
    public static void main(String[] args) {
        String s = "hello (split this) string";
        String reg = "[ ](?=[^\\)]*?(?:\\(|$))";
        System.out.println(Arrays.toString(s.split(reg)));
    }
}

// output: [hello, (split this), string]

You can find the above implementation here.

Community
  • 1
  • 1
  • thanks man, it worked. But, i don't know what is the meaning of regex. You gave an explanation but i don't understand what is meant by this `(?:)` symbol. – Logesh-0304 Jun 01 '20 at 13:33
  • I updated the answer @Logesh-0304. You can also read the meaning of above regex in the demo which I provided. On right hand side of [regex101](https://regex101.com/) you can see the explanation tab; there also you can notice the same. –  Jun 01 '20 at 13:40
  • Thankyou , now i understand some about regex – Logesh-0304 Jun 02 '20 at 14:44
1

Try "(?:[^ (]+|(?>\\([^()]*\\))|\\()+(?=[ ]|$)"

notes

  • use to match all elements instead of split
  • will match unbalanced parens and use a atomic group on paren like ( kk hh )

demo