This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to remove special/hidden characters from a string with a regex expression Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to remove special/hidden characters from a string with a regex expression" Watch "How to remove special/hidden characters from a string with a regex expression" New topic
Author

How to remove special/hidden characters from a string with a regex expression

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
I want to remove the special/hidden characters from a string using a regex expression. Is there an easy way to get all escape characters or do I just need to add them all



If I need to add them all what are they?? Is there somewhere I can look to find a list?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

You're the person who decided you had to remove special, or hidden, or escape characters. I don't believe that Unicode defines any of those terms. So yes, you're going to have to come up with a definition or a list or something. Was this based on some real-life requirement?
Joseph Rose
Greenhorn

Joined: Feb 02, 2010
Posts: 2
Something like:



should work to remove anything that is not a letter or number, at least the hard ones. Just check the HEX values in an ASCII table to remove them in bulk like the sample here.

Hope this helps.

EDIT: I used the table at http://www.asciitable.com/ and the app at http://gskinner.com/RegExr/ to test this.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

Regex has a character class, "\p{Graph}", which defines all visible characters. So, using the negate of that character class, should target all whitespaces, control characters, and other out of range characters.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

I'm going to guess that Mr. Williams won't want to consider the space character as a special character to be deleted, but I could be wrong.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to remove special/hidden characters from a string with a regex expression
 
Similar Threads
need java regex pattern
Regex splitting on carat symbol
Problem using java.util.regex.Pattern class to match a pattern.
Validating the file path using regex
How to escape regular expression's special characters using javascript?