• 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

String - Remove dashes from text

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

I have a long String and this is an example of it :


I want to delete all dashes like the second dash here (dashes that have text before & after them, but not space characters like the first dash). Doing this in regular expressions would be great, so i want to say :
"Find any dashes that have text before and after them(but no spaces) and if found then delete that dashes from the text".

Is there a method to do this ?

Thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hesham Gneady wrote:I want to delete all dashes like the second dash here (dashes that have text before & after them, but not space characters like the first dash). Doing this in regular expressions would be great, so i want to say :
"Find any dashes that have text before and after them(but no spaces) and if found then delete that dashes from the text".



Sure. Regular expressions can do this with absolutely no difficulty whatsoever. What have you tried so far?

Henry
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know how to find the text using regular expressions, but i need a method in java to remove the dash from the found text.
For the example i wrote above, here is the regex that will grab the text "between-text" :



Now how to remove the dash ?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hesham Gneady wrote:Now how to remove the dash ?



You can use the replaceAll() method to change the matched text -- in this case, change the dash to something else, such as a space. As for not deleting whats around the dash, you can change those components of the match to either a look-ahead or look-behind.

Henry
 
Hesham Gneady
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Henry, it's my first time to know about "lookahead" & "lookbehind" ... That was really helpful
Here is my regex :
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic