Sudhanshu Umalkar
Sudhanshu Umalkar

Reputation: 4202

REGEX help required - breaking up line

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

I want to break this into groups depending on brackets.

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

Can somebody please provide the regular expression for the example in Java?

Upvotes: 0

Views: 86

Answers (2)

Dr.Kameleon
Dr.Kameleon

Reputation: 22820

Try this one : (Demo : http://regexr.com?33u8e)

(\(.+?\))

Code Example (in PHP) :

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

Another approach : try splitting (using positive look-behind)

(?<=\))\s

EDIT: Final answer (in Java)

String[] parts = str.split("(?<=\\))\\s");

Upvotes: 1

Vishal Suthar
Vishal Suthar

Reputation: 17194

Here you go: \((.+?)\)

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

Upvotes: 0

Related Questions