R stringr按出现的顺序提取关键字

R stringr extracting keywords in the order that they appear in

我已经做了一些搜索,但找不到解决方案,欢迎其他 packages/methods。 我正在从句子中提取一系列职位头衔,以便从他们的传记中建立人们职业生涯的时间表。我正在使用 stringr 包来提取这些职位名称,问题是它们不是按照它们在句子中出现的顺序出现的,而是按照它们在我的列表中的顺序出现的。下面是一个简化的示例:

sentence <- "He was a chief executive officer, chairman of the board and 
president"
Job <- list("chairman of the board","chief executive officer", "president")
str_extract_all(sentence,unlist(Jobb))

这个输出是:

[[1]]
[1] "chairman of the board"

[[2]]
[1] "chief executive officer"

[[3]]
[1] "president"

理想情况下,这些职位名称应按照它们出现的顺序排列(即董事会主席和首席执行官交换职位)我不能只更改职位列表的顺序,因为每句话都会不同。 在此先感谢您的帮助

您可以将可能的标题作为一个正则表达式而不是多个不同的正则表达式提供。将它们与正则表达式 "or" 连接起来,即 |:

> str_extract_all(sentence, paste0(unlist(Job), collapse = "|"))
[[1]]
[1] "chief executive officer" "chairman of the board"   "president"