• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex problem

 
mic ta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I would like to remove everything what is not a character eg.
from "one,two' three . four ! Five" to "onetwothreefourFive"

I have started to write this code, but how to finish it.


How is possible to remove everything what is not a character with regex?

Best regards,
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

You could also use this:
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]
repeated above
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ireneusz Kordal wrote:
Which is built into String as well as a shortcut:
 
mic ta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the solutions.

I need just the string and the Rob Prime shortcut looks very short compare to Ireneusz Kordal solution.

Are there any speed differences between the two solutions?

EDIT: Is it possible that the regular expression convert also the match to lower case?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only wanted it to match lowercase characters then you would just remove A-Z in the regular expression, so your result would look something like this:



-Hunter
 
mic ta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant if an upper-case character is found than this character should convert to lower case, but I understand now that I am looking for non character and therefore I solve the problem with equalsIgnoreCase.
 
Henry Wong
author
Marshal
Pie
Posts: 21005
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mic ta wrote:I meant if an upper-case character is found than this character should convert to lower case, but I understand now that I am looking for non character and therefore I solve the problem with equalsIgnoreCase.


Converting to lower case is best done via the toLowerCase() method, instead of the regex related replaceAll() method. Regex is not a cure all.


mic ta wrote:EDIT: Is it possible that the regular expression convert also the match to lower case?


To answer your question.... Yes. with regex, it is possible, but not with the replaceAll() method, as it is too high level (actually, you can, but doing 26 different passes for replacements is silly).

To use regex, to convert to lower case, you will have to use the find(), appendReplacement(), and appendTail() methods.

Henry


Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic