• 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

Regex expression

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

I have a string say "ABC++SXR++DUMMY.png" its a file name here, i need to split the string with the delimiter '++' so i have done it in a simple way i.e.



which gives me an array containing [ABC,SXR,DUMMY.png] here, i want like [ABC,SXR,DUMMY] without the extension part. What would be the regex for achieving the same or should i be doing again the substring on it to remove the .ext from the list?

Please suggest
 
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
It would be possible to do it as part of the split but I would remove the extension before the split.

That line of code cannot be the line you are using since the regex does not have quotes round it and you should be using "\\" and not "//" ! Also, if you did use "\\++" this would split

into the same set of values as


Is that what you want?

 
ms raaghu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

How would you remove the extension, is that like doing the substring till "." or how?

cant regex be applied to extract the string till the .ext? so that the result would be achieved at a shot

Please suggest
 
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

ms raaghu wrote:
How would you remove the extension, is that like doing the substring till "." or how?

cant regex be applied to extract the string till the .ext? so that the result would be achieved at a shot

Please suggest



I would probably use String.replaceFirst() but if you are new to regex you might find it easier to use String.lastIndexOf('.') and String.substring().
 
Ranch Hand
Posts: 296
Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ms raaghu wrote:I have a string say "ABC++SXR++DUMMY.png" ..., i want like [ABC,SXR,DUMMY]


The expression:

will give you strings separated by any number of "+" or file extension.
 
ms raaghu
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks surlac,

it worked skipping . part successfully.
 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this as well




Thanks...
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The original poster hasn't been answering the questions that have been asked, so I don't think we really know the requirements. However, the original question said that '++' was the delimiter, suggesting that the number of + signs is not arbitrary, and a single '+' should not be considered a delimiter. If that's the case, then the correct regex would be
 
surlac surlacovich
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khuzema Dharwala wrote:


Have you run this code by yourself?
In this case any number will be as a delimiter. Also double ++ makes redundant empty strings because it doesn't recognize ++ as a single delimiter (it is 2 separate delimiters).
Also, what if extension is in upper case?
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
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