开发者

R regex store variables?

开发者 https://www.devze.com 2023-03-21 07:31 出处:网络
Is it possible to use stored variables in R\'s regex? For example I want to remove quotes around decimal numbers in开发者_如何学Python the following string s = \"\\\"Bob\\\",\\\"1\\\",\\\"Mary\\\",\\

Is it possible to use stored variables in R's regex?

For example I want to remove quotes around decimal numbers in开发者_如何学Python 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.

Also as a side question does R have the \d support? (it throws an error when i try it) Thanks


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])

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.

Edit: @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.

0

精彩评论

暂无评论...
验证码 换一张
取 消