首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提取两个符号之间的字符串。

提取两个符号之间的字符串。
EN

Stack Overflow用户
提问于 2021-10-10 01:55:48
回答 2查看 40关注 0票数 1

我在编辑一份凌乱的参考名单。我想从一年到下一段时间提取字符串。原文:

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、认知复杂性与社会性。

我使用以下代码

代码语言:javascript
复制
str_extract(df1$References, pattern = "(?<=\\).).*(?=\\.)")

而所提取的文本在第一次之后并没有停止“。它返回:

1]“为什么国家失败:权力、繁荣和贫穷的起源”,皇冠书刊

2“网络方法”,载于“政策过程的理论”(第二版)。剑桥,MA: Westview出版社。

3“认知复杂性与社会性”,“英国社会与临床心理学杂志”,8,211-216

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-10 01:59:06

考虑使用regex模式,该模式匹配一个或多个字符,这些字符不是点([.]+),后面是\\。这将继承).和一个包含在regex查找范围内的空格(\\s)。

代码语言:javascript
复制
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." 

数据

代码语言:javascript
复制
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))
票数 0
EN

Stack Overflow用户

发布于 2021-10-10 03:02:14

sub的基本R选项。提取(number)之后的文本,直到下一个句号。

代码语言:javascript
复制
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" 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69511756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档