• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need regex to solve

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. Can any body please say what regex i should use to split the string. The string is a= "a,'b,c',d,e,'f,g,h',i" ; i need the out put in this way.
a
b,c
d
e
f,g,h
i
How can i do that i know that it is possible using regular expressions. But i have tried a lot and gone through many books. But i dint get any thing in my head. Regular expressions is like unknown language to me. Some one please help me out.


Thanks and Regards.
alexander
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I don't have a quick answer for you now, I just wanted to mention this website which has a lot of useful information about regular expressions: http://www.regular-expressions.info - It's not specifically about regular expressions in Java, but it contains a lot of useful information and examples. Regular expressions are a subject which is hard to understand at first, but it's worth investing some time to learn them, because they are very powerful and can be just the right tool you need in some situations.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeeb

Use the below regular expression.



Regards,
Antany
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Care to explain how that works? Otherwise Adeeb will not learn anything.

Edit: I thought that couldn't be right. It doesn't split the d,e into two separate tokens.
 
Antany Vasanth
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rob,

You are right. Thanks Rob

Hello Adeeb,

I am not sure whether It can be done using Single regular expression with in String's split() method(If I am wrong correct me). But It can be done using loops. I think that's not what you want.

Regards
Antany.
 
Marshal
Posts: 79233
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you match '.+' somehow, then split the remainder on commas?
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adeeb alexander wrote:Hi all. Can any body please say what regex i should use to split the string. The string is a= "a,'b,c',d,e,'f,g,h',i" ; i need the out put in this way.
a
b,c
d
e
f,g,h
i
How can i do that i know that it is possible using regular expressions. But i have tried a lot and gone through many books. But i dint get any thing in my head. Regular expressions is like unknown language to me. Some one please help me out.


Thanks and Regards.
alexander



In plain English, a regex solution might sound like this: split on a comma only if that comma has zero, or an even number of single quotes ahead of it:



You can then remove the single quotes from the tokens. Using this approach, you don't remove the single quotes at the same time since that will break the logic of "counting" an even (or uneven) number of quotes ahead of it.

If you are wondering how to do it in one go, then it will get (a bit?) more complicated. You would then have to use a bit of alternation: split on comma-quote OR quote-comma OR the pattern described earlier:

 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic