0

我有类似的字符串

when (update [page1] [desc]) and match ([desc] is not null), then (Rule:setvalue; SourceValue:[ign [desc]]; TargetField:[desc2])

有没有人有好的方法来提取 3 个字符串:

(update [page1] [desc])
([desc] is not null)
(Rule:setvalue; SourceValue:[ign [desc]]; TargetField:[desc2])

3 部分可以是任何字符。Java regex 也可以很好地实现。但是添加关键字'when','and match','then'可能会引起轰动。

4

2 回答 2

0

你可以试试这个模式:

String regex = "\\([^)]++\\)";
于 2013-05-01T10:53:26.090 回答
0
public static void main(String[] args) {
        String str = "when (update [page1] [desc]) and match ([desc] is not null), then (Rule:setvalue; SourceValue:[ign [desc]]; TargetField:[desc2])";
        Pattern pattern = Pattern.compile("\\((.+?)\\)");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }

    }

输出:

(update [page1] [desc])
([desc] is not null)
(Rule:setvalue; SourceValue:[ign [desc]]; TargetField:[desc2])

如果您不想在输出中使用大括号:

改变:

System.out.println(matcher.group());

至:

System.out.println(matcher.group(1));
于 2013-05-01T10:41:07.967 回答