aspose file tools*
The moose likes Beginning Java and the fly likes regex problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regex problem" Watch "regex problem" New topic
Author

regex problem

mic ta
Greenhorn

Joined: May 13, 2009
Posts: 28
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

Joined: Jun 21, 2008
Posts: 423
Try this:

You could also use this:
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
[edit]
repeated above
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Ireneusz Kordal wrote:
Which is built into String as well as a shortcut:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
mic ta
Greenhorn

Joined: May 13, 2009
Posts: 28
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

Joined: Mar 13, 2009
Posts: 492

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
mic ta
Greenhorn

Joined: May 13, 2009
Posts: 28
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
wood burning stoves
 
subject: regex problem