将RegEx从JavaScript转换为Java [英] Convert RegEx from JavaScript to Java

查看:85
本文介绍了将RegEx从JavaScript转换为Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我试图用,分隔一个字符串,它不在'['或']'里面。我有一个适用于JavaScript的RegEx,但无法将其转换为Java语法。

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

例句:

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

它适用于 http://refiddle.com 但是当我尝试在Java中使用RegEx(在Eclipse下)时,我收到一条错误消息:

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


索引10
附近未封闭的字符类,(?![^ [] *])

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

所有我确实删除了开头的'/'和结尾的/ g,我无法翻译语法。

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

最好的方法是什么?

推荐答案

嵌套方括号支持的更新



由于你还需要支持嵌套的方括号,并且在方括号内应该忽略逗号,你需要一个简单的解析器来收集你需要的文本块。

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;
}

并将其用作

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);
} 

示例Java代码

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.

在Java字符类中,] [ 必须进行转义,这与JavaScript中只需转义] 符号(在字符类中)不同。

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

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

这是 IDEONE演示

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

请注意,无论是Java还是JS,] ,在角色类之外,不必转义。

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

这篇关于将RegEx从JavaScript转换为Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆