Regular Expression on string with braces in Java

Posted on 2009-05-12
Last Modified: 2013-12-17
Hi guys,

I hope you could help me with these.  I need a regular expression that could split up the string below




Currently I have "\\{([.*]*)[^}]*}"   but it doesn't work. It will also include the groups inside, which i don't want

Thanks in advance
Question by:xanbi
LVL 84

Accepted Solution

ozo earned 500 total points
ID: 24371754
LVL 40

Expert Comment

ID: 24375825
I would suggest that for recursive grammars, regular expressions are not usually adequate. You can usually write a "good enough" solution, but it will not be bulletproof for the whole grammar. There is a limit to how you can employ lookahead and lookbehind in a non-recursive approach, which regular expressions are.

Recursive descent parsers can be written for special cases, and I have provided a sample that will not descend into the nested {} as you asked. In a real parser we would usually return tree nodes and parse until there is no nesting left, but since you want the nesting to be preserved past 1 level, here is a sample that works, maybe you can use it.

// Simple recursive descent parser for matching nested tokens

public class Main {

	  public static void main(String [] args) {

	       String s = "{adfsadfd}{sdads}{asdf{asdf}{asdf}}{asdfsdf{dads}}";

	       String token;

	       int beginIndex = 0;

	       while((token = match(s.substring(beginIndex))) != null) {


	    	   beginIndex += token.length();



	   public static String match(String s) {

	      int i = 0; char ch;

	      if(s == null || s.length() == 0)

	    	  return null;

	      String token = "";

	      if((ch =s.charAt(i++)) != '{') // not a valid start pattern

	    	  return null;

	      token += ch;

	      while(i < s.length()) {

	    	  ch = s.charAt(i);

	    	  if(ch == '}') { // end of top level {} pattern

	    		  token += ch;

	    		  return token;


	    	  else if(ch != '{') { // non bracket, add to the token

	    		  token += ch;



	    	  else { // nested {, so recursively match and concat sub-strings

	    		  String subtoken = match(s.substring(i));

	    		  if(subtoken != null) {

	    			  token += subtoken;

	    			  i += subtoken.length();





	      return token;



Open in new window


Author Closing Comment

ID: 31580846
Thank you very much.  I accepted ozo solution because I should use regular expression prior to making scan through the string.


Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Specific format 21 181
JItbit AD intergration 4 76
Need help decoding the following expressions 1 28
Syntax Error 2 45
A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
Let’s list some of the technologies that enable smooth teleworking. 
This video shows how use content aware, what it’s used for, and when to use it over other tools.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now