Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex split characters and return delimiters

 
Sugantha Jeevankumar
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers.. I need to do a regex split on a string like "aaa bbb {ccc ddd} eee {fff}" and split this into tokens like here,

aaa bbb
{ccc ddd}
eee
{fff}


The approach I have attempted is to use the "opening brace-any character-closing brace" trio as the delimiter. And to get to my kind of output I need to return the delimiter! The closest I have got to the result is with this: "(?=(\\{([^}]*)\\}))" using the positive lookahead in an attempt to return the delimiter. This yields me this result,

aaa bbb
{ccc ddd} eee
{fff}


Googling around I learnt that Perl makes it easy to return delimiters while pattern matching but I am yet to find anything straightforward in java. If not possible, I may need to resort to manual parsing of the string. Please send in your suggestions. Thanks all !!
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Split on (zero width positive look-behind for }) OR (zero width positive look ahead for {).

edit You haven't indicated your requirement for any leading or trailing spaces: include or exclude?
 
Richard Tookey
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<deleted/> I just worked out what you are trying to do and Darryl has given you the answer.
 
Sugantha Jeevankumar
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can ignore the leading and trailing whitespaces. The regex pattern I have tried out in my previous post is positive lookahead, I guess. Do you mean my delimiter would only be "{" if I use the zero-width positive lookahead. I am completely out of depth in regex, so if "zero-width positive lookahead" is the way to go, I would search more on this.

@Richard
I was just working out a reply to your post
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Split on (zero width positive look-behind for }) OR (zero width positive look ahead for {).

 
Richard Tookey
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Sugantha Jeevankumar
@Richard
I was just working out a reply to your post

I was asking for a clarification of your requirements when I realized that Darryl had understood what I had not and had provided a solution. My post was therefore at best redundant and probably irrelevant so I deleted it.
 
Sugantha Jeevankumar
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works great.. and now I also understand how.. Thanks Darryl
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic