0

(city = 'pune' AND country = 'india') OR (division = 'it') AND (resource = 'someresource' )

我想根据括号将其分成几组。

第 1 组 -(city = 'pune' AND country = 'india')
第 2 组 -(division = 'it')
第 3 组 -(resource = 'someresource' )

有人可以提供Java中示例的正则表达式吗?

4

2 回答 2

1

试试这个:(演示:http ://regexr.com?33u8e )

(\(.+?\))

代码示例(在 PHP 中):

<?php
     $subject = "(city = 'pune' AND country = 'india') OR (division = 'it') AND (resource = 'someresource' )";
     $pattern = '/(\(.+?\))/';
     preg_match($pattern, $subject, $matches);
     print_r($matches);
?>

另一种方法:尝试拆分(使用积极的后视)

(?<=\))\s

编辑:最终答案(Java)

String[] parts = str.split("(?<=\\))\\s");
于 2013-02-27T08:27:26.913 回答
0

干得好:\((.+?)\)

string mystring = "(city = 'pune' AND country = 'india') OR (division = 'it') AND (resource = 'someresource' )";
Match results = Regex.Match(mystring, @"\((.+?)\)",RegexOptions.IgnoreCase);

foreach (Group g in results.Groups)
{
    string token = g.Value;
}

现场演示

于 2013-02-27T08:34:52.810 回答