2

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Nissim Nanach
  • 1,925
  • 2
  • 16
  • 18
  • 1
    No, you'll need to contact the documentation. Also notice that this hardly makes sense as a language-agnostic question, so I'll limit it to R. – Bergi Mar 21 '14 at 17:27
  • This is definitely a programming language independent question since it's just working with POSIX classes. Take a look at `pcre_compile.c`, `pcre_internal.h` & `pcre_maketables.c`. You might be able to see what they match in your locale via this [SO post](http://stackoverflow.com/questions/8921365/in-python-how-to-list-all-characters-matched-by-posix-extended-regex-space) – hrbrmstr Mar 21 '14 at 17:43

2 Answers2

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

gsub("[^[:punct:]]", "", rawToChar(as.raw(1:127)), "")
## [1] "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
Jake Burkhead
  • 6,435
  • 2
  • 21
  • 32
  • Could you give a little info on this please, particularly `rawToChar(as.raw(1:127))`. Thanks (i did look at `?`help) – user20650 Mar 21 '14 at 18:22
0

If you only need to worry about ASCII, maybe something like the following would do it (using 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
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306