正则表达式的所有​​可打印字符字符、正则表达式

2023-09-04 09:31:51 作者:止步于喜欢

有没有像\一个特殊的正则表达式语句W¯¯这表示所有可打印字符?我想验证一个字符串只包含一个字符,可以打印 - 即不包含像\ B(钟),或者为null,等任何键盘上是好的,所以是UTF字符。

Is there a special regex statement like \w that denotes all printable characters? I'd like to validate that a string only contains a character that can be printed--i.e. does not contain ASCII control characters like \b (bell), or null, etc. Anything on the keyboard is fine, and so are UTF chars.

如果没有特别的声明,我怎么能在一个正则表达式指定此?

If there isn't a special statement, how can I specify this in a regex?

推荐答案

好吧,如果你Google一下,你会发现一些答案的时候了。有一个POSIX字符类指定 [:打印:] 应该匹配可打印字符, [:CNTRL:] 为控制字符。请注意,这些匹配codeS整个ASCII表,所以他们可能不适合用于匹配其它编码

Well, if you Google it you'll find some answers right away. There is a POSIX character class designation [:print:] that should match printable characters, and [:cntrl:] for control characters. Note that these match codes throughout the ASCII table, so they might not be suitable for matching other encodings.

如果做不到这一点,前pression [\ x00- \ X1F] 将匹配通过ASCII控制字符,虽然再次,这可能是打印在其他编码

Failing that, the expression [\x00-\x1f] will match through the ASCII control characters, although again, these could be printable in other encodings.