Note that there are some explanatory texts on larger screens.

plurals
  1. PONon-greedy Pattern Expression
    primarykey
    data
    text
    <p>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 '<strong>a</strong>' the expression:</p> <pre><code>sed -r 's/([^a]*)(a)/\ (1)\1(2)\2(ALL)&amp;(END)/g' &lt;&lt;&lt;"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)... </code></pre> <p>from which the pattern (with reference to Friedl) might be:</p> <ul> <li>[ <strong><em>normal* closing</em></strong> ]</li> </ul> <p>Moving on to a real multi-character '<strong>ab</strong>' delimiter:</p> <pre><code>sed -r 's/([^a]*)((a[^b]*)*)(ab)/\ (1)\1(2)\2(3)\3(4)\4(ALL)&amp;(END)/g' &lt;&lt;&lt;"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)... </code></pre> <p>from which the pattern might be:</p> <ul> <li>[ <strong><em>normal* (special*)* closing</em></strong> ] </li> </ul> <p>For the subsequent '<strong>abc</strong>' delimiter the <strong><em>special</em></strong> expression can be extended to:</p> <pre><code>(a[^b]*)*(ab[^c]*)* </code></pre> <ol> <li>Is this correct?</li> <li>Can it be proved?</li> <li>Can the <strong><em>special</em></strong> expression be simplified?</li> <li>Are there better/more efficient expressions for this? n.b. I'm not using perl's non-greedy '*?' operator and avoiding alternation.</li> <li>Where might I find reference material to this type of problem (Friedl alluded but stopped short of a published solution).</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload