Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter string for unwanted characters using regex?

Tags:

java

regex

Basically , I am wondering if there is a handy class or method to filter a String for unwanted characters. The output of the method should be the 'cleaned' String. Ie:

String dirtyString = "This contains spaces which are not allowed"  String result = cleaner.getCleanedString(dirtyString); 

Expecting result would be:

"Thiscontainsspaceswhicharenotallowed" 

A better example:

String reallyDirty = " this*is#a*&very_dirty&String"  String result = cleaner.getCleanedString(dirtyString); 

I expect the result to be:

"thisisaverydirtyString" 

Because, i let the cleaner know that ' ', '*', '#', '&' and '_' are dirty characters. I can solve it by using a white/black list array of chars. But I don't want to re-invent the wheel.

I was wondering if there is already such a thing that can 'clean' strings using a regex. Instead of writing this myself.

Addition: If you think cleaning a String could be done differently/better then I'm all ears as well of course

Another addition: - It is not only for spaces, but for any kind of character.

like image 261
Stefan Hendriks Avatar asked Feb 09 '11 13:02

Stefan Hendriks


People also ask

How do you filter characters in a string?

In Java 8 and above, use chars() or codePoints() method of String class to get an IntStream of char values from the given sequence. Then call the filter() method of Stream for restricting the char values to match the given predicate.

How do I remove a specific character from a string in regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

Can you filter a string?

We can also use filter() with a string as an iterable sequence and can filter out characters from it.


1 Answers

Edited based on your update:

dirtyString.replaceAll("[^a-zA-Z0-9]","") 
like image 182
jzd Avatar answered Oct 11 '22 19:10

jzd