gpt4 book ai didi

regex - 从字符串中提取匹配的单词

转载 作者:行者123 更新时间:2023-12-04 19:00:07 25 4
gpt4 key购买 nike

我有一个数据库结构 - 下面的缩写版本

structure(list(sex1 = c("totalmaleglobal", "totalfemaleglobal", 
"totalglobal", "totalfemaleGSK", "totalfemaleglobal",
"totalfemaleUN")), .Names = "sex1", row.names = c(NA, 6L),
class="data.frame")

我想提取单词“total”、“totalmale”、“totalfemale”

如何做到这一点?

我用下面的代码尝试了正则表达式
pattern1=c("total")
pattern2=c("totalmale")
pattern3=c("totalfemale")

daly$sex <- str_extract(daly$sex1,pattern1)
daly$sex <- str_extract(daly$sex1,pattern2)
daly$sex <- str_extract(daly$sex1,pattern3)

但它给了我 NA。

最佳答案

也许

library(stringr)
daly$sex <- str_extract(daly$sex1,paste(rev(mget(ls(pattern = "pattern\\d+"))), collapse="|"))
daly
# sex1 sex
# 1 totalmaleglobal totalmale
# 2 totalfemaleglobal totalfemale
# 3 totalglobal total
# 4 totalfemaleGSK totalfemale
# 5 totalfemaleglobal totalfemale
# 6 totalfemaleUN totalfemale

关于regex - 从字符串中提取匹配的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39406224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com