条件正则过滤筛选 V2

迭代提炼抽象自条件正则过滤筛选 V1

概要:

  • 基础语法实现的PRCE单语句正则查找,捕获可选模式的左右部分。

功能:

  • 依据模式S,条件性(不)执行——查找(可选的)首个目标模式T,捕获其左侧$1、右侧$2文本。

实现:

  • 使用PCRE正则的基础语法(未使用实验性、高版本功能,兼容性高):
    • Lazy Quantifier惰性求值;
    • Positive Lookahead前向匹配断言;
    • (?:T|$) 推进对可选目标模式的全文检测,避免回溯(Backtracking)。

参考:

 1 (?#skip match process when S exist, or remove optional left most target T, capture left and right part of T.
 2 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.
 3 skip exist:)^(?!.*S)(?#
 4 captrue prefix :)^(.*?)(?#
 5 optional target to remove:)(?:T|$)(?#
 6 captrue postfix:)(.*?)$(?#
 7 Test string:
 8 aa
 9 aaT
10 Tcc
11 aaTcc
12 aaTbbTcc
13 aaS
14 aaTS
15 TccS
16 aaTccS
17 Saa
18 SaaT
19 STcc
20 SaaTcc
21 Substitution: $1$2N
22 Return:
23 aaN
24 aaN
25 ccN
26 aaccN
27 aabbTccN
28 aaS
29 aaTS
30 TccS
31 aaTccS
32 Saa
33 SaaT
34 STcc
35 SaaTcc)
View Code

 维护最新版本见create or change one optional part to another (58bd3z/1)

原文地址:https://www.cnblogs.com/RobertL/p/14136714.html