gpt4 book ai didi

java - 任何字母、数字和 "normal"字符的正则表达式

转载 作者:太空狗 更新时间:2023-10-29 16:21:08 24 4
gpt4 key购买 nike

我将字节接收到一个方法中,我想通过串行发送它们,但我只想发送有效字节,(即 a-zA-Z0-9"!£$%^&*()-_=+ )、诸如此类的东西、空格、换行等。我只想以任意顺序和任意次数过滤掉任何带有重音符号或 � 的字符。

像这样包括所有带有 | 的字符是否可行?

^[a-z|A-Z|0-9|\\s|-<other characters>]*

或者,正确的表达方式是什么?

因此,如果一个字符串包含“exit����”,我只想发送“exit”,绝不会发送无效字符,而是发送其他所有字符。

public void write(byte[] bytes, int offset, int count) {
String str;
try {
str = new String(bytes, "ASCII");
Log.d(TAG, "data received in write: " +str );
//^[a-z|A-Z|0-9|\s|-]*
//test here, call next line on any character that is valid
GraphicsTerminalActivity.sendOverSerial(str.getBytes("ASCII"));
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "exception" );
e.printStackTrace();
}
// appendToEmulator(bytes, 0, bytes.length);
}

编辑:我尝试了 [^\x00-\x7F],这是 ascii 字符的范围....但是 � 符号仍然通过,很奇怪。

最佳答案

尝试使用像[\x20-\x7E] 这样的模式 这些是the printable characters 的ASCII 码.

顺便说一下,我假设您是在询问有关 ASCII 的信息,因为这就是您在问题中解析的方式。

关于java - 任何字母、数字和 "normal"字符的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14526649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com