需要REGEX帮助 - 分手

时间:2013-02-27 08:24:13

标签: java regex

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

我想根据括号将其分组。

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

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

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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;
}

Live Demo