非贪婪模式表达
我一直在阅读 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)...
其中的模式可能是:
- [ 正常* (特殊*)* 结束< /em> ]
对于后续'abc' 分隔符特殊表达式可以扩展为:
(a[^b]*)*(ab[^c]*)*
- 这是正确的吗?
- 可以证明吗?
- 特殊表达式可以简化吗?
- 有没有更好/更有效的表达方式?注意我没有使用 Perl 的非贪婪 '*?'操作员并避免交替。
- 我在哪里可以找到此类问题的参考资料(弗里德尔提到过,但没有发布解决方案)。
I've been reading "Mastering Regular Expressions" by Friedl and trying to devise a common non-greedy pattern expression for a string that is delimited by a word.
Starting from basics where the delimited word is just a single character 'a' the expression:
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)...
from which the pattern (with reference to Friedl) might be:
- [ normal* closing ]
Moving on to a real multi-character 'ab' delimiter:
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)...
from which the pattern might be:
- [ normal* (special*)* closing ]
For the subsequent 'abc' delimiter the special expression can be extended to:
(a[^b]*)*(ab[^c]*)*
- Is this correct?
- Can it be proved?
- Can the special expression be simplified?
- Are there better/more efficient expressions for this? n.b. I'm not using perl's non-greedy '*?' operator and avoiding alternation.
- Where might I find reference material to this type of problem (Friedl alluded but stopped short of a published solution).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)