*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
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: 18541
    
    8

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: 18538
    
  40

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: 18541
    
    8

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://aspose.com/file-tools
 
subject: How to remove special/hidden characters from a string with a regex expression
 
Similar Threads
Problem using java.util.regex.Pattern class to match a pattern.
How to escape regular expression's special characters using javascript?
Validating the file path using regex
need java regex pattern
Regex splitting on carat symbol