我正在尝试在RMarkdown中为正则表达式创建一个基本的引用表,并且我在尝试将字符串连接在一起时遇到了一些麻烦。我不确定我是否应该使用``而不是'“来字面指定这些字符串,但我很困难。好像我在语法方面遇到了很多错误。任何帮助,将不胜感激。感谢。
这是表格在降价代码中的样子:
POSIX Class Name| Description |Examples ------------- | ------------------------|------------------------ [:alpha:] |Alphanumeric characters |[[:alpha:][:digit:]] or [A-z09] [:punct:] |Punctation Characters |! \ \" # $ % & '( ) * + , - . / : ; ? @ [ \ \ ] ^ _`{ | }~
但是,其中一些字符难以在字符串中呈现(例如空字符)。下面是我尝试在数据框中执行此示例表的代码。
#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\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,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的格式。
非常感谢任何帮助。感谢。
答案 0 :(得分:1)
在?Syntax
和?Quotes
中描述的未列出的项目之前替换所有单个反斜杠(带有双倍反斜杠)之后的R特殊内容,如“\ n”和“\ t”,并省略最后一个class_name和description向量中的三个(因为它们没有相应的项例例矢量,所以可以制作合法的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
显示该字符,则只能看到该字符,但对于类的项目没有cat
- 方法 - "data.frame"
:
> print("\\")
[1] "\\"
> cat("\\")
\