【发布时间】:2020-12-22 15:22:05
【问题描述】:
我正在寻找我似乎无法获得的特定正则表达式模式:
神秘地:
pattern <- "[1 word|no word][this is][1-3 words max]"
text <- c("this guy cannot get a mortgage, this is a fake application", "this is a new application", "hi this is a specific question", "this is real", "this is not what you are looking for")
str_match("pattern", text)
我想要的输出是:
[1]FALSE #cause too many words in front
[2]TRUE
[3]TRUE
[4]TRUE
[5]FALSE #cause too many words behind it
这应该是可行的,但我正在努力解决正则表达式中的单词和最大数量 谁能帮我解决这个问题?
【问题讨论】:
-
问R为什么要加上python标签?
-
Datacrust,StackExchange 标签推荐系统还可以,但偶尔会提供不好的建议。在这种情况下,您允许它建议 python,这在问题中不建议/支持。请更加注意使用的标签; “更多”可以引起更多关注,因此更有可能获得答案,但不相关的标签可能会引起反对票、赞成票和/或只是负面回应。
-
此外,虽然熟悉 R 包生态系统的人可能很容易推断您正在使用
stringr包,但依赖于您并不明智在那。请明确使用非基础 R 包。如果您还没有访问过它们,有几个地方可以阅读关于如何在 SO 上很好格式化可重复和独立的问题:参考:stackoverflow.com/q/5963269、minimal reproducible example 和 stackoverflow.com/tags/r/info .谢谢!