我一直在阅读Friedl的“掌握正则表达式”,并尝试为一个由单词分隔的字符串设计一个常见的非贪婪模式表达式。 从基础开始,分隔的单词只是一个单词“ a ”表达式:
sed -r 's/([^a]*)(a)/\
(1)\1(2)\2(ALL)&(END)/g' <<<"xaxxaxxxaxxx...aa..."
(1)x(2)a(ALL)xa(END)
(1)xx(2)a(ALL)xxa(END)
(1)xxx(2)a(ALL)xxxa(END)
(1)xxx...(2)a(ALL)xxx...a(END)
(1)(2)a(ALL)a(END)...
模式(参考Friedl)可能是:
转到真正的多字符“ ab ”分隔符:
sed -r 's/([^a]*)((a[^b]*)*)(ab)/\
(1)\1(2)\2(3)\3(4)\4(ALL)&(END)/g' <<<"xabxxabxxxabxxx...abxxx...aabxxx...axxx...aaabxaabaxabaxaxabxaxaabxxaaabaaxxab..."
(1)x(2)(3)(4)ab(ALL)xab(END)
(1)xx(2)(3)(4)ab(ALL)xxab(END)
(1)xxx(2)(3)(4)ab(ALL)xxxab(END)
(1)xxx...(2)(3)(4)ab(ALL)xxx...ab(END)
(1)xxx...(2)a(3)a(4)ab(ALL)xxx...aab(END)
(1)xxx...(2)axxx...aa(3)axxx...aa(4)ab(ALL)xxx...axxx...aaab(END)
(1)x(2)a(3)a(4)ab(ALL)xaab(END)
(1)(2)ax(3)ax(4)ab(ALL)axab(END)
(1)(2)axax(3)axax(4)ab(ALL)axaxab(END)
(1)x(2)axa(3)axa(4)ab(ALL)xaxaab(END)
(1)xx(2)aa(3)aa(4)ab(ALL)xxaaab(END)
(1)(2)aaxx(3)aaxx(4)ab(ALL)aaxxab(END)...
模式可能是:
对于后续的“ abc ”分隔符, 特殊 表达式可以扩展为:
(a[^b]*)*(ab[^c]*)*
答案 0 :(得分:1)