Regular Expressions 101

Community Patterns

create or change one optional part to another

1

Regular Expression
PCRE (PHP <7.3)

/
(?#skip match process when S exist, or remove optional left most target T, capture left and right part of T. Use case: if N is not exist, modify/change T to N, or create N. If any create or modify write operated, appending output N to right. skip exist:)^(?!.*S)(?# captrue prefix :)^(.*?)(?# optional target to remove:)(?:T|$)(?# captrue postfix:)(.*?)$(?# Test string: aa aaT Tcc aaTcc aaTbbTcc aaS aaTS TccS aaTccS Saa SaaT STcc SaaTcc Substitution: $1$2N Return: aaN aaN ccN aaccN aabbTccN aaS aaTS TccS aaTccS Saa SaaT STcc SaaTcc)
/
gm

Description

Skip match process when S exist, or remove optional left most target T, capture left and right part of T. Use case: if N is not exist, modify/change T to N, or create N. If any create or modify write operated, appending output N to right using subsitution "$1$2N".

Submitted by anonymous - 3 years ago