Convert RegEx from JavaScript to Java

匿名 (未验证) 提交于 2019-12-03 02:28:01

问题:

OK, so I am trying to split a String by ", " which are not inside '[' or ']'. I have a working RegEx for JavaScript but have been unable to convert it to Java syntax.

JS RegEX:

/,(?![^[]*])/g 

Example sentence:

ex1 , [ex2 , ex3 ] , ex 4 , ex 4, [ex , ex ] 

It works fine on http://refiddle.com but when I try and use the RegEx in Java (under Eclipse) I get an error saying:

Unclosed character class near index 10 ,(?![^[]*])

All I did was remove the '/' at the beginning and the "/g" at the end and I have been unable to translate the Syntax.

What would be the best way to achieve this?

回答1:

Update for nested square bracket support

Since you need to also support nested square brackets, and the comma should be ignored inside the square brackets, you need a simple parser to collect the chunks of text you need.

public static List<String> splitWithCommaOutsideBrackets(String input) {     int BracketCount = 0;     int start = 0;     List<String> result = new ArrayList<>();     for(int i=0; i<input.length(); i++) {         switch(input.charAt(i)) {         case ',':             if(BracketCount == 0) {                 result.add(input.substring(start, i).trim());// Trims the item!                 start = i+1;             }             break;         case '[':             BracketCount++;             break;         case ']':             BracketCount--;             if(BracketCount < 0)                  return result; // The BracketCount shows the [ and ] number is unbalanced             break;         }     }     if (BracketCount > 0)         return result; // Missing closing ]     result.add(input.substring(start).trim()); // Trims the item!     return result; } 

And use it as

String s = "ex1 , [ex2 , ex3 ] , [ hh3 , rt5 , w3 [ bn7 ] ] , ex 4 , ex 4, [ex , ex ]"; List<String> res = splitWithCommaOutsideBrackets(s); for (String t: res) {     System.out.println(t); }  

Output of the sample Java code:

ex1 [ex2 , ex3 ] [ hh3 , rt5 , w3 [ bn7 ] ] ex 4 ex 4 [ex , ex ] 

Note that trimming items is not necessary.

Also, where I return result, you may want to add code throwing an exception rather than returning the result as it is at that moment.

Original answer

In Java character classes, ] and [ must be escaped, unlike in JavaScript where you only have to escape ] symbol (inside the character class).

String pat = ",(?![^\\[]*])";                     ^^ 

Here is an IDEONE demo:

String s = "ex1 , [ex2 , ex3 ] , ex 4 , ex 4, [ex , ex ]"; String pat = ",(?![^\\[]*])"; String[] result = s.split(pat); System.out.println(Arrays.toString(result)); 

Note that neither in Java, nor in JS, the ], outside the character class, does not have to be escaped.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!