=ArrayFormula(filter(A1:A5,regexmatch(A1:A5,"(?i)^"&"(.*?\bbob\b)(.*?\bcat\b)"&".*$")))
如果Bob在cat之前,它将找到Bob和cat。
如果我尝试使用前瞻?=,即Google表格失败。
=ArrayFormula(filter(A1:A5,regexmatch(A1:A5,"(?i)^"&"(?=.*?\bbob\b)(?=.*?\bcat\b)"&".*$")))
我不想在字符串中使用 '|' 替换(重复和反转),因为输入的单词可能超过两个,这样替换会变得指数级别复杂。
以下是测试搜索数组(每行都包含一个字符串的单元格)...
Bob ate the dead cat
The cat ate live bob
No cat ate live dog
Bob is dead
Bob and the cat are alive
...以及我想要的预期结果。
Bob ate the dead cat
The cat ate live bob
Bob and the cat are alive
一旦我整理好了正则表达式,最终的解决方案将是一个用户输入文本框,他们只需输入必须在字符串中找到的单词,例如 'Bob cat'。我认为我可以将这个输入字符串拆分成单独的单词,并将它们连接到上面的表达式中,然而,如果有一种'最佳实践'的方法来完成这个任务,我想听听。
and(regexmatch(A1:A5,'(?i)\bBob\b'), regexmatch(A1:A5, '(?i)\bcat\b'))
。 - Wiktor Stribiżew