0

我在 Sphinx 中使用 regexp_filter 替换术语

在大多数情况下,我可以这样做,例如拼写错误很容易:

regexp_filter = Backround => Background

甚至使用捕获组表示法进行交换:

regexp_filter = (Left)(Right) => \2\1

但是,在使用模式匹配来查找要替换的给定单词时,我遇到了更多麻烦:

 regexp_filter = (PatternWord1|PatternWord2)\W+(?:\w+\W+){1,6}?(SearchTerm)\b => NewSearchTerm

其中 NewSearchTerm 将是我只想替换 \2 的术语(仅留下 \1 和模式的其余部分)。所以

因此,如果我当时有文字'Pizza and Taco Parlor'

regexp_filter = (Pizza)\W+(?:\w+\W+){1,6}?(Parlor)\b => Store

将转换为'Pizza and Taco Store'

我知道在这种情况下 SearchTerm 是 /2 但不确定如何转换。我知道我可以附加 eg /2s 使其成为复数,但实际上我该如何替换它,因为它只是一个由多个捕获组组成的捕获组,而我只想替换该组?

4

1 回答 1

0

所以,如果我理解这个问题。您有一个符合以下条件的字符串:

  1. 从 PattenWord1 或 PatternWord2 开始
  2. 紧跟大写单词
  3. 可能后跟另一个介于 1 到 6 个字符之间的单词 -- 建议使用 [Az] 而不是 \w+\W+
  4. 其次是“搜索词”

让我们以此为基准:

PatternWord1HelloSearchTerm

而且您只想从字符串中替换 SearchTerm。

因此,您需要围绕要保留的所有内容创建另一个模式组:

regexp_filter = ((PatternWord1|PatternWord2)\W+(?:\w+\W+){1,6}?)(SearchTerm)\b => \1World

您的模式组匹配将是:

  1. PatternWord1你好
  2. 模式字1
  3. 搜索词

你的结果是:

PatternWord1HelloWorld

于 2015-12-26T19:08:50.760 回答