R正则表达式存储变量? [英] R regex store variables?
问题描述
是否可以在 R 的正则表达式中使用存储变量?
Is it possible to use stored variables in R's regex?
例如,我想删除以下字符串中十进制数字周围的引号 s = "\"Bob\",\"1\",\"Mary\",\"2\""
- 在大多数语言中,您可以执行诸如 sub("\"(\d)\"","$1",s)
之类的操作,但我似乎找不到 R 中的功能.任何帮助都会不胜感激.
For example I want to remove quotes around decimal numbers in the following string s = "\"Bob\",\"1\",\"Mary\",\"2\""
- in most languages you could do something like sub("\"(\d)\"","$1",s)
but I cannot seem to find the capability in R. Any help would be greatly appreciated.
还有一个附带问题,R 是否支持 \d
?(当我尝试时它会抛出错误)谢谢
Also as a side question does R have the \d
support? (it throws an error when i try it) Thanks
推荐答案
我相信这通常被称为回引用.在 R 中,您可以使用 \\1 \\2 等.
I believe this is usually called back referencing. In R, you can use \\1 \\2, etc.
re.examples <- c(
'What_are_we_doing?',
'Woe, that a young fowl should fly the coop',
'2011/12/24',
'Subject: More information, then less important stuff.'
)
sub("([0-9]+)/([0-9]+)/([0-9]+)","Year is \\1 Month is \\2 Day is \\3",re.examples[3])
sub("^([A-Za-z ]+): ([A-Za-z ]+), ([A-Za-z ]+).$","\\2",re.examples[4])
<小时>
我不确定 R 中的 \d 支持.无论如何我通常只使用 [0-9],因为我知道它有效并且我发现它更易于阅读.
I'm not sure about \d support in R. I generally just use [0-9] anyway, since I know it works and I find it easier to read.
@Andrie 和@Richie Cotton 都在评论中提供了两个建议,为了完整起见,我将在此处包含这些建议.[:digits:] 有效,但在我看来,[0-9] 的可读性很少.\\d 也可以.
@Andrie and @Richie Cotton both offered two suggestions in comments, which I will include here for completeness. [:digits:] works, but to my mind offers little in readability over [0-9]. \\d works as well.
这篇关于R正则表达式存储变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!