• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

regex problem  RSS feed

 
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,
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

You could also use this:
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]
repeated above
 
Sheriff
Posts: 21459
96
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?
 
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.
 
author
Sheriff
Posts: 23588
138
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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!