• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Regex split characters and return delimiters

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That works great.. and now I also understand how.. Thanks Darryl
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic