[英]How do I replace a comma separated list of strings with the first occurrence?
WHERE MATCH (COALESCE(f1, f2, f3)) AGAINST (?) > 0
WHERE MATCH (COALESCE(f1)) AGAINST (?) > 0
Wanted: 通缉:
WHERE MATCH (f1) AGAINST (?) > 0
WHERE MATCH (f1) AGAINST (?) > 0
Need to substritute COALESCE(f1, f2, f2, ...)
with just f1
, that is the first string if multiple string are present (separated by ,
) or just the string itself. 需要substritute
COALESCE(f1, f2, f2, ...)
与刚刚f1
,即如果多个字符串存在于第一串(通过分离,
)或仅字符串本身。
#\s*match\s*\((\s*coalesce\s*\((.+)\s*\))\s*\)\s+against#/i
And i'm capturing both what's inside MATCH
(1, what's need to be replaced) and what's inside COALESCE
(2, the replacement). 而且我正在捕获
MATCH
内部的内容(1,需要替换的东西)和COALESCE
内部的内容(2,替换的东西)。
How do I substituite 1 with the first value inside 2? 如何用2内的第一个值替换1?
Replace should start at COALESCE
but the previous part must match. 替换应从
COALESCE
开始,但前一部分必须匹配。 Use \\K for resetting after (where replacing should start). 之后使用\\ K进行重置 (应从此处开始替换)。 So the first part could look like
MATCH\\s*\\(\\K
here starts replacing and pattern continues: \\s*COALESCE\\s*\\(\\s*([^,)]+)
... capturing in first capture group. 因此,第一部分看起来像
MATCH\\s*\\(\\K
在这里开始替换,模式继续: \\s*COALESCE\\s*\\(\\s*([^,)]+)
...在第一个捕获组中捕获。
And the optional part (?:,[^)]*)?
还有可选部分
(?:,[^)]*)?
to meet a closing bracket. 碰到一个结束括号。 For the
AGAINST
part a lookahead can be used: (?=\\)\\s*AGAINST)
. 对于
AGAINST
部分,可以使用前瞻 : (?=\\)\\s*AGAINST)
。 So the whole pattern could be: 因此整个模式可能是:
/MATCH\s*\(\K\s*COALESCE\s*\(\s*([^,)]+)(?:,[^)]*)?\)(?=\)\s*AGAINST)/i
And replace with captured $1
. 并替换为捕获的
$1
。 Test at regex101.com 在regex101.com上测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.