File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regex split characters and return delimiters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex split characters and return delimiters" Watch "Regex split characters and return delimiters" New topic
Author

Regex split characters and return delimiters

Sugantha Jeevankumar
Ranch Hand

Joined: Jun 06, 2007
Posts: 92
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 !!


SCJP 5.0
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

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?


luck, db
There are no new questions, but there may be new answers.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 958
    
  10

<deleted/> I just worked out what you are trying to do and Darryl has given you the answer.
Sugantha Jeevankumar
Ranch Hand

Joined: Jun 06, 2007
Posts: 92
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

Joined: May 03, 2008
Posts: 4522
    
    5

Darryl Burke wrote:Split on (zero width positive look-behind for }) OR (zero width positive look ahead for {).

Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 958
    
  10

[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

Joined: Jun 06, 2007
Posts: 92
That works great.. and now I also understand how.. Thanks Darryl
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex split characters and return delimiters
 
Similar Threads
Report in JSP
Priority Queue Ordering Question
array
help - what's the difference: axis following and axis following-sibling?
How to split a string by the whitespace but ignoring '\n'?