php - How do I replace a comma separated list of strings with the first occurrence? -
where match (coalesce(f1, f2, f3)) against (?) > 0 match (coalesce(f1)) against (?) > 0 wanted:
where match (f1) against (?) > 0 match (f1) against (?) > 0 need substritute coalesce(f1, f2, f2, ...) f1, first string if multiple string present (separated ,) or string itself.
#\s*match\s*\((\s*coalesce\s*\((.+)\s*\))\s*\)\s+against#/i and i'm capturing both what's inside match (1, what's need replaced) , what's inside coalesce (2, replacement).
how substituite 1 first value inside 2?
replace should start @ coalesce previous part must match. use \k resetting after (where replacing should start). first part match\s*\(\k here starts replacing , pattern continues: \s*coalesce\s*\(\s*([^,)]+)... capturing in first capture group.
and optional part (?:,[^)]*)? meet closing bracket. against part lookahead can used: (?=\)\s*against). whole pattern be:
/match\s*\(\k\s*coalesce\s*\(\s*([^,)]+)(?:,[^)]*)?\)(?=\)\s*against)/i and replace captured $1. test @ regex101.com
Comments
Post a Comment