File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String - Remove dashes from text

 
Hesham Gneady
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 :
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic