This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Java regex for removing brackets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java regex for removing brackets" Watch "Java regex for removing brackets" New topic
Author

Java regex for removing brackets

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3057
    
  38

Consider the following String

String s= "copy (x copy(y,z), left(s,4) ) and update(m)";

Any idea of removing every bracketed expression from the above String, using regular expressions? I need the final result as
"copy (x copy(y,z), left(s,4) ) and update(m)". I'm wondering to do that with String.replaceAll(String,String) method, but couldn't make an appropriate regular expression for that.
I know we can simply do that by manually coding it with loops and conditions. Just interested to know if it is possible to do so with regular expressions. Any thoughts?


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40


Doing it in a single pass will not be easy with regex. This is due to the "nested" requirement. This is not to say it is impossible, though.

One option, is to not deal with the "nested" requirement. And run it through many passes.

s.replaceAll("\\([^\\(]*\\)", "");

After one pass.... "copy (x copy(y,z), left(s,4) ) and update(m)";
After two pass.... "copy (x copy(y,z), left(s,4) ) and update(m)";


But personally, it is probably easier to just not use regex. By keeping a count, of the depth of the nests, as you parse, you can do it in a single pass.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3057
    
  38

Thanks Henry, that's a good suggestion.
The given regex didn't work properly but it worked after a small modification as "\\([^\\(^\\)]*\\)"
Thanks again,
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

Devaka Cooray wrote:
The given regex didn't work properly but it worked after a small modification as "\\([^\\(^\\)]*\\)"


Oops... forgot to mention that I didn't actually try it out...


BTW, I didn't try this one out either...

s.replaceAll("\\([^\\(]*?\\)", "");

Henry
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3057
    
  38

Henry Wong wrote:s.replaceAll("\\([^\\(]*?\\)", "");


I just forgot to use that reluctant quantifier
Both "\\([^\\(^\\)]*\\)" and "\\([^\\(]*?\\)" works properly.

Devaka
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

FYI The back slashes in the character class are not required in your case. Hence, a slightly cleaner version would be:
"\\([^()]*\\)" and "\\([^(]*?\\)"

Cheers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java regex for removing brackets