removing all non-printing characters by regex

peter.murray.rust :

I have files which contain non-printing characters such as \u2066-\u2069 (directional formatting) and \u2000-\u2009 (spaces of various widths, e.g.   ). Is it possible to remove (or replace) them by using a (Java) regex? (\\s+ does not work with the above). I don't want to build this myself as I don't know what characters I might get.

Wiktor Stribiżew :

All the characters you provided belong to the Separator, space Unicode category, so, you may use

s = s.replaceAll("\\p{Zs}+", " ");

The Zs Unicode category stands fro space separators of any kind (see more cateogry names in the documentation).

To replace all horizontal whitespaces with a single regular ASCII space you may use

s = s.replaceAll("\\h+", " ");

As per Java regex documentation,

\h     A horizontal whitespace character: [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]

If you want to shrink all Unicode whitespace to a single space

s = s.replaceAll("(?U)\\s+", " ");

The (?U) is an embedded flag option equal to the Pattern.UNICODE_CHARACTER_CLASS option passed to the Pattern.compile method. Without it, \s matches what \p{Space} matches, i.e. [ \t\n\x0B\f\r]. Once you pass (?U), it will start matching all whitespace chars in the Unicode table.

To tokenize a string, you may split directly with

String[] tokens = s.split("\\p{Zs}+");
String[] tokens = s.split("\\h+");
String[] tokens = s.split("(?U)\\s+");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=321865&siteId=1