如何列出正则表达式字符类的成员,例如[:punct:]?

时间:2022-05-17 18:16:25

For example, I know from documentation such as

例如,我从文档中了解到

http://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html

that

[:punct:]

includes

包括

! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~.

but I would like to check from the command line (in my case, of R, but probably similar in bash etc.), and also list out [:alpha:] etc.

但我想从命令行检查(在我的情况下,R,但在bash等可能类似),并列出[:alpha:]等。

2 个解决方案

#1


1  

 grep("[[:punct:]]", unlist(strsplit(rawToChar(as.raw(1:127)), "")), value = TRUE)
 ## [1] "!"  "\"" "#"  "$"  "%"  "&"  "'"  "("  ")"  "*"  "+"  ","  "-"  "."  "/" 
 ## [16] ":"  ";"  "<"  "="  ">"  "?"  "@"  "["  "\\" "]"  "^"  "_"  "`"  "{"  "|" 
 ## [31] "}"  "~" 

gsub("[^[:punct:]]", "", rawToChar(as.raw(1:127)), "")
## [1] "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"

#2


0  

If you only need to worry about ASCII, maybe something like the following would do it (using bash):

如果您只需要担心ASCII,可能会像以下一样(使用bash):

$ for n in {0..127}; do awk '{ printf("%c", $0); }' <<< $n | grep '[[:punct:]]' | tr '\n' ' '; done
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

$ for n in {0..127}; do awk '{ printf("%c", $0); }' <<< $n | grep '[[:alnum:]]' | tr '\n' ' '; done
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z

#1


1  

 grep("[[:punct:]]", unlist(strsplit(rawToChar(as.raw(1:127)), "")), value = TRUE)
 ## [1] "!"  "\"" "#"  "$"  "%"  "&"  "'"  "("  ")"  "*"  "+"  ","  "-"  "."  "/" 
 ## [16] ":"  ";"  "<"  "="  ">"  "?"  "@"  "["  "\\" "]"  "^"  "_"  "`"  "{"  "|" 
 ## [31] "}"  "~" 

gsub("[^[:punct:]]", "", rawToChar(as.raw(1:127)), "")
## [1] "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"

#2


0  

If you only need to worry about ASCII, maybe something like the following would do it (using bash):

如果您只需要担心ASCII,可能会像以下一样(使用bash):

$ for n in {0..127}; do awk '{ printf("%c", $0); }' <<< $n | grep '[[:punct:]]' | tr '\n' ' '; done
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

$ for n in {0..127}; do awk '{ printf("%c", $0); }' <<< $n | grep '[[:alnum:]]' | tr '\n' ' '; done
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z