在 R 中如何判断字符串是否包含转义序列?

发布于 2024-10-09 05:25:56 字数 148 浏览 0 评论 0原文

我在 R 中有一个字符串,例如 x <- "c:\tmp\rest.zip"。我怎样才能检测到它里面有转义序列呢? \t 和 \r?我们 DOS/Windows 人员有使用 R 不喜欢的反斜杠的习惯,我正在编写一个函数,我希望能够保护用户免受自身侵害。

谢谢。

I have a string in R, e.g. x <- "c:\tmp\rest.zip". How can I detect that it has escape sequences in it, vis. \t and \r? Us DOS/Windows guys have a habit of using backslashes that R doesn't like and I'm writing a function where I would like to be able to protect the user from themselves.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌能量女王 2024-10-16 05:25:56

grep 模式中双反斜杠是成功之路:

 xtxt <- c("test\n", "of\t", "escapes")
 grep("\\n|\\t", xtxt)
# [1] 1 2

搜索控制字符的另一种方法:

 grep("[[:cntrl:]]", xtxt)
#[1] 1 2

Doubling of the back-slashes in the grep pattern is the path to success:

 xtxt <- c("test\n", "of\t", "escapes")
 grep("\\n|\\t", xtxt)
# [1] 1 2

Another way to be to search for control characters:

 grep("[[:cntrl:]]", xtxt)
#[1] 1 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文