• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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 ??

Thanks,
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marimuthu Madasamy
Ranch Hand
Posts: 72
Java Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

References:
http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html
http://www.regular-expressions.info/tutorial.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic