替换正则表达式模式中的单个术语
Replacing a single term in a regex pattern
我在 Sphinx 中使用 regexp_filter 来替换术语
在大多数情况下我可以这样做,例如拼写错误很容易:
regexp_filter = Backround => Background
甚至使用捕获组表示法进行交换:
regexp_filter = (Left)(Right) =>
但是,在使用模式匹配查找我要替换的给定单词时,我遇到了更多麻烦:
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 但不确定如何转换。我知道我可以附加例如/2s 使其成为复数,但实际上我该如何替换它,因为它只是一个包含多个的捕获组,而我只想替换该组?
所以,如果我理解这个问题。您有一个符合以下条件的字符串:
- 从 PattenWord1 或 PatternWord2 开始
- 紧跟一个大写单词
- 可能后跟另一个介于 1 到 6 个字符之间的单词 -- 建议使用 [A-z] 而不是 \w+\W+
- 其次是"SearchTerm"
让我们以此为基准:
PatternWord1HelloSearchTerm
并且您只想替换字符串中的 SearchTerm。
所以你需要另一个模式组围绕你想保留的一切:
regexp_filter = ((PatternWord1|PatternWord2)\W+(?:\w+\W+){1,6}?)(SearchTerm)\b => World
您的模式组匹配为:
- PatternWord1你好
- 模式字1
- 搜索词
您的结果将是:
PatternWord1HelloWorld
我在 Sphinx 中使用 regexp_filter 来替换术语
在大多数情况下我可以这样做,例如拼写错误很容易:
regexp_filter = Backround => Background
甚至使用捕获组表示法进行交换:
regexp_filter = (Left)(Right) =>
但是,在使用模式匹配查找我要替换的给定单词时,我遇到了更多麻烦:
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 但不确定如何转换。我知道我可以附加例如/2s 使其成为复数,但实际上我该如何替换它,因为它只是一个包含多个的捕获组,而我只想替换该组?
所以,如果我理解这个问题。您有一个符合以下条件的字符串:
- 从 PattenWord1 或 PatternWord2 开始
- 紧跟一个大写单词
- 可能后跟另一个介于 1 到 6 个字符之间的单词 -- 建议使用 [A-z] 而不是 \w+\W+
- 其次是"SearchTerm"
让我们以此为基准:
PatternWord1HelloSearchTerm
并且您只想替换字符串中的 SearchTerm。
所以你需要另一个模式组围绕你想保留的一切:
regexp_filter = ((PatternWord1|PatternWord2)\W+(?:\w+\W+){1,6}?)(SearchTerm)\b => World
您的模式组匹配为:
- PatternWord1你好
- 模式字1
- 搜索词
您的结果将是:
PatternWord1HelloWorld