in reply to Re^3: greedy subexpression between two nongreedy ones
in thread greedy subexpression between two nongreedy ones

I see now my problem statement was a little sloppy: I failed to use the same sample text in my first two examples. Namely, I inadvertently omitted the trailing comma in the first example.

And sure enough, the answer above (or rather, the three variations on the answer) works on the incorrect example, but still fails on the correct one.

$ echo ,abcdefg,abcdefg | perl -pe 's/,(?:.*?(cd))?.*?,/=$1=/' =cd=abcdefg $ echo ,abcdefg,abcdefg, | perl -pe 's/,(?:.*?(cd))?.*?,/=$1=/' =cd=abcdefg,
works either way when the "cd" appears between the first two anchors, but if it doesn't:
$ echo ,abcefg,abcdefg | perl -pe 's/,(?:.*?(cd))?.*?,/=$1=/' ==abcdefg $ echo ,abcefg,abcdefg, | perl -pe 's/,(?:.*?(cd))?.*?,/=$1=/' =cd=
it gives me what I'm looking for only when the final "," is missing. With it, the expression goes back to running past the anchor and stealing from the next blob of text.

I need to study the answer below to see if it does what I need, but I had high hopes for the ones above, because I like their simplicity. :-)

Replies are listed 'Best First'.
Re^5: greedy subexpression between two nongreedy ones
by Anonymous Monk on Jun 02, 2015 at 20:43 UTC

    Phony problems lead to phony solutions... Please show the exact problem.

      In my twenty-five years of programming, my experience in consulting people with questions is that they prefer to have a narrowed example that demonstrates the problem concisely, rather than have it buried in code that has no bearing on the problem. Most take this as a sign that the asker has done some work already in trying to find an answer, and look upon that favorably. (And more than once, I've found my own solution just in the process of framing the question for someone else.) I apologize if this approach is inappropriate here in the monastery, and thank everyone who responded despite my phony presentation.
Re^5: greedy subexpression between two nongreedy ones
by Anonymous Monk on Jun 02, 2015 at 21:10 UTC
    s#,(?:(?:(?!,).)*(cd))?(?:(?!,).)*,#=@{[$1 // '']}=#

    Are there any more requirements you're not telling us?