我有一个需要分割的话和结束标记(某些类型的标点符号)。奇怪的管道(“|”)可以算作结束标记。我有代码在结束标记上的单词,直到我尝试添加管道。添加管道使得每个字符都是strsplit
。转义它导致错误。我如何在正则表达式中包含管道?逃脱管道(“|”)在正则表达式
x <- "I like the dog|."
strsplit(x, "[[:space:]]|(?=[.!?*-])", perl=TRUE)
#[[1]]
#[1] "I" "like" "the" "dog|" "."
strsplit(x, "[[:space:]]|(?=[.!?*-\|])", perl=TRUE)
#Error: '\|' is an unrecognized escape in character string starting "[[:space:]]|(?=[.!?*-\|"
的结果,我想:
#[[1]]
#[1] "I" "like" "the" "dog" "|" "." #pipe is an element
我总是犹豫不决,把R上的正则表达式的问题正则表达式的标签,因为你从其他语言regexers,虽然答案是相似的,他们不重叠。 –