【发布时间】:2018-01-09 20:47:10
【问题描述】:
我正在尝试从一些文件名中提取一个字符串,以便稍后用作变量。
文件名如下所示:
c("./Vote/Академический vote 1.xls", "./Vote/Академический vote 2.xls",
"./Vote/Академический vote 3.xls", "./Vote/Алексеевский в городе Москве vote 1.xls",
"./Vote/Алексеевский в городе Москве vote 2.xls", "./Vote/Алтуфьевский vote 1.xls",
"./Vote/Алтуфьевский vote 2.xls", "./Vote/Алтуфьевский vote 3.xls",
"./Vote/Арбат vote 1.xls", "./Vote/Арбат vote 2.xls", "./Vote/Аэропорт vote 1.xls",
"./Vote/Аэропорт vote 2.xls", "./Vote/Аэропорт vote 3.xls", "./Vote/Бабушкинский vote 1.xls",
"./Vote/Бабушкинский vote 2.xls", "./Vote/Басманный vote 1.xls",
"./Vote/Басманный vote 2.xls", "./Vote/Басманный vote 3.xls",
"./Vote/Беговой vote 1.xls", "./Vote/Беговой vote 2.xls", "./Vote/Бескудниковский vote 1.xls",
"./Vote/Бескудниковский vote 2.xls", "./Vote/Бибирево vote 1.xls",
"./Vote/Бибирево vote 2.xls", "./Vote/Бибирево vote 3.xls")
> dput(sample(vote_files, size = 25))
c("./Vote/Лианозово vote 2.xls", "./Vote/Зюзино vote 1.xls",
"./Vote/Восточное Дегунино vote 2.xls", "./Vote/Аэропорт vote 2.xls",
"./Vote/Академический vote 1.xls", "./Vote/Замоскворечье в городе Москве vote 1.xls",
"./Vote/Обручевский vote 2.xls", "./Vote/Даниловский vote 3.xls",
"./Vote/Нагатино-Садовники vote 1.xls", "./Vote/Ново-Переделкино в городе Москве vote 1.xls",
"./Vote/Кунцево vote 2.xls", "./Vote/Текстильщики в городе Москве vote 2.xls",
"./Vote/Южное Медведково vote 1.xls", "./Vote/Западное Дегунино vote 2.xls",
"./Vote/Хамовники vote 1.xls", "./Vote/Крюково vote 1.xls", "./Vote/Беговой vote 1.xls",
"./Vote/Восточный vote 1.xls", "./Vote/Богородское vote 2.xls",
"./Vote/Некрасовка vote 2.xls", "./Vote/Косино-Ухтомский vote 1.xls",
"./Vote/Лосиноостровский vote 3.xls", "./Vote/Хорошевский vote 2.xls",
"./Vote/Бирюлево Западное vote 2.xls", "./Vote/Гольяново vote 3.xls"
)
我正在尝试使用sub 提取/Vote/ 和/vote #.xls 之间的俄语文本,如下所示
sub(x= string, pattern = ".*((?<=.//Vote//).*(?=vote)).*", replacement = "\\1", perl = T)
我必须使用环视,因为我要提取的字符串有时不止一个单词。然而,尽管当我在在线正则表达式测试器上验证时,捕获组似乎捕获了正确的文本,但 sub 调用只返回与我输入的完全相同的字符串。
这里有什么问题?或者,有没有更简单的方法来做到这一点?
【问题讨论】:
-
您在投票的每一边都使用双斜线,而只有一个斜线。
".*((?<=/Vote/).*(?=vote)).*"应该可以工作。 -
如果你执行 gsub(“\\w|[:punct:]”, “”,x) 会怎样?
标签: r regex substring backreference capturing-group