• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 72
Scala Monad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic