Regular expression to remove all non-printable characters

I want to remove all non-printable ascii characters from a string while keeping invisible. I thought this would work because spaces, \ n \ r are invisible characters, but not printable? Basically I get a byte array with characters in it, and I don't want them to be in it. So I am trying to convert it to a string, remove characters before using it as a byte array again.

Everything works fine in my code now, but now \ r and \ n are not working. What will be the correct regular expression to save them? Or is there a better way I do it?

public void write(byte[] bytes, int offset, int count) { try { String str = new String(bytes, "ASCII"); str2 = str.replaceAll("[^\\p{Print}\\t\\n]", ""); GraphicsTerminalActivity.sendOverSerial(str2.getBytes("ASCII")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return; } } 

EDIT: I tried [^ \ x00- \ x7F], which is an ascii character range .... but then the characters still pass, weird.

+6
source share
2 answers

The following regular expression will only match the printable text.

 [^\x00\x08\x0B\x0C\x0E-\x1F]* 

Next Regex will find non-printable characters

 [\x00\x08\x0B\x0C\x0E-\x1F] 

Jave Code:

 boolean foundMatch = false; try { Pattern regex = Pattern.compile("[\\x00\\x08\\x0B\\x0C\\x0E-\\x1F]"); Matcher regexMatcher = regex.matcher(subjectString); foundMatch = regexMatcher.find(); //Relace the found text with whatever you want } catch (PatternSyntaxException ex) { // Syntax error in the regular expression } 
+10
source

Here I would prefer a simpler solution. By the way, you ignored the offset and count. The solution below overwrites the original array.

 public void write(byte[] bytes, int offset, int count) { int writtenI = offset; for (int readI = offset; readI < offset + count; ++readI) { byte b = bytes[readI]; if (32 <= b && b < 127) { // ASCII printable: bytes[writtenI] = bytes[readI]; // writtenI <= readI ++writtenI; } } byte[] bytes2 = new byte[writtenI - offset]; System.arraycopy(bytes, offset, bytes2, 0, writtenI - offset); //String str = new String(bytes, offset, writtenI - offset, "ASCII"); //bytes2 = str.getBytes("ASCII"); GraphicsTerminalActivity.sendOverSerial(bytes2); } 
+1
source

All Articles