The moose likes Java in General and the fly likes What regex expression to use to split at ( ) ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What regex expression to use to split at ( ) ??" Watch "What regex expression to use to split at ( ) ??" New topic

What regex expression to use to split at ( ) ??

Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56

I have a String myString ="(1)or(2 and 3)or(4 and 5 and 6)" . I need to split string in an array such that in contains data inbetween ( ).

e.g. String[] exp = myString.split(____);

exp[0]= "1";
exp[1]= "2 and 3";
exp[2]= "4 and 5 and 6";

Can anyone please let me know the regex expression to be used for this ??

David Newton

Joined: Sep 29, 2008
Posts: 12617

Is this something it would be better to use a parser for rather than regexes? Do the statements always look exactly like that?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

The right regex makes it quite easy. You need just three parts to it:
- positive lookbehind for (. Note that you must escape the (
- a reluctant quantifier for the contents
- positive lookahead for ). Again, escape it.

You can see how to translate this into a regex on the Javadocs of java.util.regex.Pattern. Don't use String.split though, use Pattern and Matcher.

How To Ask Questions How To Answer Questions
Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

Using Regex, you need a pattern with three constructs,
1) A matching opening parenthesis
2) Match any character until it matches a closing parenthesis
3) A matching closing parenthesis

To get you start with:
1) To match a character, you just need to have that character in the pattern string. Watch out for meta characters which need to be escaped.
2) To match any character, the meta character '.' (dot) is used.
3) To match a character zero or more times, the meta character '*' (asterisk) is used
4) To extract a subset of data from the regex, you can use something called 'groups'. In your case, the parenthesis' are used to just match and you are interested in the data in between them; so you can capture the data as a group.


- Marimuthu Madasamy
I agree. Here's the link:
subject: What regex expression to use to split at ( ) ??
It's not a secret anymore!