我在编辑一份凌乱的参考名单。我想从一年到下一段时间提取字符串。原文:
1 "Acemoglu,D,& Robinson,J.A.“(2012),”为什么国家失败:权力、繁荣和贫困的起源“,皇冠书刊。
2 "Adam,S,& Kriesi,H. (2007)。网络方法“,载于Sabatier,P.A.(编辑),”政策过程的理论“(第二版)。剑桥,MA:韦斯特维尤出版社。”3“亚当斯-韦伯,J.R. (1969)。认知复杂性和社会性。英国社会和临床心理学杂志,8,211-216。
我想提出以下几点:
1“为什么国家失败:权力、繁荣和贫穷的根源。”
2“网络方法”。
3、认知复杂性与社会性。
我使用以下代码
str_extract(df1$References, pattern = "(?<=\\).).*(?=\\.)")而所提取的文本在第一次之后并没有停止“。它返回:
1]“为什么国家失败:权力、繁荣和贫穷的起源”,皇冠书刊
2“网络方法”,载于“政策过程的理论”(第二版)。剑桥,MA: Westview出版社。
3“认知复杂性与社会性”,“英国社会与临床心理学杂志”,8,211-216
发布于 2021-10-10 01:59:06
考虑使用regex模式,该模式匹配一个或多个字符,这些字符不是点([.]+),后面是\\。这将继承)、.和一个包含在regex查找范围内的空格(\\s)。
library(stringr)
library(tibble)
str_extract(df1$References, "(?<=\\)\\.\\s)[^.]+\\.")
[1] "Why nations fail: The origins of power, prosperity, and poverty." "The network approach."
[3] "Cognitive complexity and sociality." 数据
df1 <- structure(list(References = c("Acemoglu, D., & Robinson, J. A. (2012). Why nations fail: The origins of power, prosperity, and poverty. Crown Books.",
"Adam, S., & Kriesi, H. (2007). The network approach. In Sabatier, P. A. (ed.), Theories of the policy process (2nd Ed.). Cambridge, MA: Westview Press.",
"Adams-Webber, J. R. (1969). Cognitive complexity and sociality. British Journal of Social and Clinical Psychology, 8, 211-216."
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-3L))发布于 2021-10-10 03:02:14
带sub的基本R选项。提取(number)之后的文本,直到下一个句号。
x <- c("Acemoglu, D., & Robinson, J. A. (2012). Why nations fail: The origins of power, prosperity, and poverty. Crown Books.",
"Adam, S., & Kriesi, H. (2007). The network approach. In Sabatier, P. A. (ed.), Theories of the policy process (2nd Ed.). Cambridge, MA: Westview Press.",
"Adams-Webber, J. R. (1969). Cognitive complexity and sociality. British Journal of Social and Clinical Psychology, 8, 211-216.")
sub('.*?\\(\\d+\\)\\.\\s*(.*?)\\..*', '\\1', x)
#[1] "Why nations fail: The origins of power, prosperity, and poverty"
#[2] "The network approach"
#[3] "Cognitive complexity and sociality" https://stackoverflow.com/questions/69511756
复制相似问题