用 kable 打印正则表达式 Table

Print out Regular Expression Table with kable

我正在尝试在 RMarkdown 中为正则表达式创建一个基本参考 table,但我在尝试将字符串连接在一起时遇到了一些问题。我不太确定我是否应该使用 `` 而不是 "" 从字面上指定这些字符串,但我很困惑。似乎我在语法方面不断遇到大量错误。任何帮助,将不胜感激。谢谢

这是 table 在降价代码中的样子:

POSIX Class Name| Description             |Examples
-------------   | ------------------------|------------------------ 
[:alpha:]       |Alphanumeric characters  |[[:alpha:][:digit:]] or [A-z09]
[:punct:]       |Punctation Characters    |! \ \" # $ % & '( ) * + , - . / : ;  ? @ [ \ \ ] ^ _`{ | }~

但是,其中一些字符很难在字符串中呈现(例如空字符)。下面是我尝试在数据框中执行此示例 table 的代码。

#Create Charatctor class table
class_name <- c("[:alnum:]","[:alpha:]","[:ascii:]","[:blank:]","[:cntrl:]","[:digit:]","[:graph:]","[:lower:]","[:print:]",
"[:punct:]","[:space:]","[:upper:]","[:xdigit:]")
description <- c("Alphanumeric characters","Alphabetic characters","ASCII characters","Space and tab","Control characters",
"Digits","Visible characters (anything except spaces and control characters)","Lowercase letters","Visible characters and spaces (anything except control characters)","Punctuation and symbols.","All whitespace characters, including line breaks",
"Uppercase letters","Hexadecimal digits")
examples <- c(`[[:alpha:][:digit:]] or [A-z0-9]`,
              `[[:lower:][:upper:]] or [A-z]`,
              `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`,
              `[ \t]`,
              "\nor\r,[\x[=11=][=11=]-\x1F\x7F]",
              `or \d: digits, 0 1 2 3 4 5 6 7 8 9, equivalent to [0-9]`,
              `[:alnum:] and [:punct:]`,
              `[a-z]`,
              `[[:alnum:][:punct:]\s]`,
              `! \ \" # $ % & '( ) * + , - . / : ; < = > ? @ [ \ \ ] ^ _`{ | }~` )

char_class <- data.frame(class_name,description,examples)
names(char_class) <- c("Class Name","Description","Examples")

#View the Table
kable(char_class, col.names = names(char_class), align = c('c','l'), caption = "Character Class Examples")

我遇到的错误:

Error: '\x' used without hex digits in character string starting ""\nor\r,[\x"
Error: nul character not allowed (line 5)

我正在尝试做的部分工作是为 R 中的正则表达式编写一个参考指南,但是除了使用普通降价外,很难打印出这些字符,但我想获得数据框中的数据,如果可能的话使用 kable 的格式。

如有任何帮助,我们将不胜感激。谢谢

?Syntax?Quotes 中描述的“\n”和“\t”等未列出的项目之前替换所有单反斜杠(双反斜杠)并省略class_name 和描述向量的最后三个(因为它们没有对应的示例向量项,所以可以制作合法的 R 数据框:

class_name <- c("[:alnum:]","[:alpha:]","[:ascii:]","[:blank:]","[:cntrl:]","[:digit:]","[:graph:]","[:lower:]","[:print:]",
"[:punct:]","[:space:]","[:upper:]","[:xdigit:]")
description <- c("Alphanumeric characters","Alphabetic characters","ASCII characters","Space and tab","Control characters",
"Digits","Visible characters (anything except spaces and control characters)","Lowercase letters","Visible characters and spaces (anything except control characters)","Punctuation and symbols.","All whitespace characters, including line breaks",
"Uppercase letters","Hexadecimal digits")
examples <- c('[[:alpha:][:digit:]] or [A-z0-9]',
              '[[:lower:][:upper:]] or [A-z]',
              'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
              '[ \t]',
              "\nor\r,[\x\0\0-\x1F\x7F]",
              'or \d: digits, 0 1 2 3 4 5 6 7 8 9, equivalent to [0-9]',
              '[:alnum:] and [:punct:]',
              '[a-z]',
              '[[:alnum:][:punct:]\s]',
              '! \ \" # $ % & \\' ( ) * + \, - . / : ; < = > ? @ [ ] ^ _ { | } ~ ' )

char_class <- data.frame(class_name[1:10],description[1:10],examples)
names(char_class) <- c("Class Name","Description","Examples")
#
> char_class
   Class Name                                                        Description
1   [:alnum:]                                            Alphanumeric characters
2   [:alpha:]                                              Alphabetic characters
3   [:ascii:]                                                   ASCII characters
4   [:blank:]                                                      Space and tab
5   [:cntrl:]                                                 Control characters
6   [:digit:]                                                             Digits
7   [:graph:] Visible characters (anything except spaces and control characters)
8   [:lower:]                                                  Lowercase letters
9   [:print:] Visible characters and spaces (anything except control characters)
10  [:punct:]                                           Punctuation and symbols.
                                                                Examples
1                                       [[:alpha:][:digit:]] or [A-z0-9]
2                                          [[:lower:][:upper:]] or [A-z]
3                   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
4                                                                  [ \t]
5                                        \nor\r,[\x\0\0-\x1F\x7F]
6               or \d: digits, 0 1 2 3 4 5 6 7 8 9, equivalent to [0-9]
7                                                [:alnum:] and [:punct:]
8                                                                  [a-z]
9                                                [[:alnum:][:punct:]\s]
10 ! \ \" # $ % & \' ( ) * + \, - . / : ; < = > ? @ [ ] ^ _ { | } ~ 

R print 函数(就是上面显示的)将反斜杠显示为“\”。 “\”的字符值包含单个字符,即反斜杠。如果你用 cat 显示它,你将只看到那个字符,但是 class-"data.frame":

的项目没有 cat-方法
> print("\")
[1] "\"
> cat("\")
\