Как преобразовать список ссылок во фрейм данных? - PullRequest
3 голосов
/ 21 мая 2019

У меня есть список ссылок, например,

references <- c(
  "Dumitru, T.A., Smith, D., Chang, E.Z., and Graham, S.A., 2001, Uplift, exhumation, and deformation in the Japanese Mt Everest, Paleozoic and Mesozoic tectonic evolution of central Africa: from continental assembly to intracontinental deformation: Journal of Neverland, v. 3, no. 192, p. 71-199.",
  "Dumitru, T.A., Smith, D., Chang, E.Z., and Graham, S.A., 2001, Uplift, exhumation, and deformation in the Japanese Mt Everest, Paleozoic and Mesozoic tectonic evolution of central Africa: from continental assembly to intracontinental deformation: Journal of Neverland, no. 3.",
  "Dumitru, T.A., Smith, D., Chang, E.Z., and Graham, S.A., 2001, Uplift, exhumation, and deformation in the Japanese Mt Everest, Paleozoic and Mesozoic tectonic evolution of central Africa: from continental assembly to intracontinental deformation: Journal of Neverland, p. 71-199."
)

Я пытался (?<=:)(?.*)(?=(v\.)|(no\.)|(p\.)), но регулярное выражение вернулось 'из континентальной сборки к внутриконтинентальной деформации: Journal of Neverland, v. 3, no. 192, стр. ' не то, что я намеревался извлечь.

(?<=:)(?:[^:].*?)(?=(, v\.)|(, no\.)|(, p\.))

То, что я ожидаю, это «Журнал Неверленда», но возвращение «от континентальной сборки к внутриконтинентальной деформации: Журнал Неверленда»

Ответы [ 3 ]

5 голосов
/ 21 мая 2019

Здесь мы просто сопоставляем текст перед последним двоеточием до следующей запятой в группе захвата

stringr::str_match(references, ": ((?!:)[^,:]*),")[,2]
# [1] "Journal of Neverland" "Journal of Neverland" "Journal of Neverland"
3 голосов
/ 21 мая 2019

Вы можете использовать

:\s*\K[^:]*?(?=,\s*(?:v|no|p)\.)

См. Демоверсию regex

Подробности

  • :- двоеточие
  • \s* - 0+ пробелов
  • \K - оператор сброса совпадения
  • [^:]*? - ноль или более символов, отличных от :, но каккак можно меньше, поскольку *? не является жадным
  • (?=,\s*(?:v|no|p)\.) - положительный прогноз, требующий ,, затем 0+ пробелов и затем следуют v, no или pс . сразу справа от текущего местоположения.

В R:

regmatches(references, regexpr(":\\s*\\K[^:]*?(?=,\\s*(?:v|no|p)\\.)", references, perl=TRUE))

См. R демо онлайн :

references <- c(
  "Dumitru, T.A., Smith, D., Chang, E.Z., and Graham, S.A., 2001, Uplift, exhumation, and deformation in the Japanese Mt Everest, Paleozoic and Mesozoic tectonic evolution of central Africa: from continental assembly to intracontinental deformation: Journal of Neverland, v. 3, no. 192, p. 71-199.",
  "Dumitru, T.A., Smith, D., Chang, E.Z., and Graham, S.A., 2001, Uplift, exhumation, and deformation in the Japanese Mt Everest, Paleozoic and Mesozoic tectonic evolution of central Africa: from continental assembly to intracontinental deformation: Journal of Neverland, no. 3.",
  "Dumitru, T.A., Smith, D., Chang, E.Z., and Graham, S.A., 2001, Uplift, exhumation, and deformation in the Japanese Mt Everest, Paleozoic and Mesozoic tectonic evolution of central Africa: from continental assembly to intracontinental deformation: Journal of Neverland, p. 71-199."
)
regmatches(references, regexpr(":\\s*\\K[^:]*?(?=,\\s*(?:v|no|p)\\.)", references, perl=TRUE))
## => [1] "Journal of Neverland" "Journal of Neverland" "Journal of Neverland"

Если вы предпочитаете решение на основе stringr, используйте

> str_extract(references, "(?<=:\\s)[^:]*?(?=,\\s*(?:v|no|p)\\.)")
[1] "Journal of Neverland" "Journal of Neverland" "Journal of Neverland"

Или, если пробел после : может быть 0 или много:

> str_match(references, ":\\s*([^:]*?)(?:,\\s*(?:v|no|p)\\.)")[,2]
[1] "Journal of Neverland" "Journal of Neverland" "Journal of Neverland"
1 голос
/ 21 мая 2019

Вот решение gsub

gsub('.*: (.*?), (?=v|no|p).*','\\1', references, perl=TRUE)
# [1] "Journal of Neverland" "Journal of Neverland" "Journal of Neverland"

В качестве альтернативы можно также использовать strsplit

vapply(strsplit(references, ': *|, *', perl=TRUE),
       function (l) {
         k <- which(startsWith(l, 'p. ') | startsWith(l, 'v. ') | startsWith(l, 'no. '))
         k <- k[1] - 1
         return (l[k]) 
       }, character (1))
# [1] "Journal of Neverland" "Journal of Neverland" "Journal of Neverland"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...