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

Split string using regular expression java

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have requirement to split a string based on Regular expression which will be of the below format.

There are 3 different type of String values -

  • ABC_1234_XL.jpg
  • XYZ_7890_SM.jpg
  • PQ_R_4567_LG.jpg


  • in the above ABC, XYZ and PQ_R are 3 types of image types which I want to extract separately and compare it with respective list of corresponding Types fetched from DB.
    So in case if I go with normal split by underscore "_", then it flunks the purpose while splitting the 3rd string.

    So I need a solution to split these string based on Regular expression, where every time the center element will [0-9] and the left would be Image Type and the right would be Image Size.
    Meaning - ImageType_ImageTypeID_ImageSize. We need to split this having the center element (imageTypeID) as base and get the left & right date by excluding the "_".
    How to achieve this with Split along with Regex?

    Help please and let me know in case if you need more info.
     
    lowercase baba
    Posts: 13091
    67
    Chrome Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Any time someone says "How do I do A with B", it makes me cringe. Maybe B is the right way to do it, but maybe not:

    How do I turn a screw using water?
    How do I sleep using the space shuttle?
    How do I mow my lawn using China?

    now...having said that....

    if the middle part is ALWAYS underscore numbers underscore...

    couldn't you set up your regex as (this is off the top of my head, not tested)

    (^.*)(_[0-9]+_)(.*$)

    I think that will give you three parts...

    the middle will give you _1234_, _7890_, and _4567_. The other parens will give you the beginning and the end. Then you can chop the underscores off the middle piece as a separate step.

    edit - you may even be able to move the underscores outside the parens and do it in one step...

    (^.*)_([0-9]+)_(.*$)
     
    Gershwin Yesudhas
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Fred,

    More than your answer, I liked the intial part much.. was ROFLing.
    thanks for that lighter notation their.

    Your regex seems to be an understandably working thing.
    Will try it out though.
    Thanks man.

    Others / Fred even, If you guys have any other suggestion too, please feel free.
    Many thanks.

     
    Gershwin Yesudhas
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    Can you please help with how to split the Regex and assign it to specific string variables?
    The regex which need to be split and assigned to 3 different string variables ---> (^.*)(_[0-9]+_)(.*$)

    Please help.

    thanks,
    Gershwin
     
    fred rosenberger
    lowercase baba
    Posts: 13091
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I've never done it in java, but the String.split() method does return a String Array. I would assume the pieces would be in there...but haven't tried or confirmed it.
     
    Gershwin Yesudhas
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's ok Fred.
    Thanks anyway.

    Let's see if someone has any idea about this.


    Regards,
    Gershwin
     
    fred rosenberger
    lowercase baba
    Posts: 13091
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Or you could try it out and see for yourself!!! That's the best way to learn, after all...
     
    if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic