in reply to Search for consecutive flush left curly braces

This regex: /}.*(?!{)}/ will do what you want I believe.

It'll match }} and }adfa} but not }{} or }asdf{asdf} etc.

Replies are listed 'Best First'.
Re^2: Search for consecutive flush left curly braces
by QM (Parson) on Jun 10, 2015 at 15:59 UTC
    Won't that mach a right-curly, followed by any-number-of-any-characters, followed by not-a-left-curly? Isn't a left-curly included in any-number-of-any-characters?

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      No, at least not as how I understand it. The (?!{) is a negative-lookahead, meaning match any characters non-greedily as long as they do NOT contain a {, before the next }.

      If I'm wrong, someone will inform me ;)

        OK, you’re wrong. SCNR. :-)

        First off, for the reason given by QM, this regex doesn’t prevent a match if there is a { character between } characters:

        2:57 >perl -wE "my $s = '}abc{def}'; say 'match' if $s =~ /}.*(?!{)}/ +;" match 2:59 >

        Second, there is no non-greedy match here: .* is greedy. But making it non-greedy doesn’t help:

        2:59 >perl -wE "my $s = '}abc{def}'; say 'match' if $s =~ /}.*?(?!{)} +/;" match 2:59 >

        A negated character class is what is needed:

        3:31 >perl -wE "my $s = '}abc{def}'; say 'match' if $s =~ /}[^{]*?}/; +" 3:31 >perl -wE "my $s = '}abc|def}'; say 'match' if $s =~ /}[^{]*?}/; +" match 3:31 >

        Hope that helps,

        Update: Tweaked the third code snippet.

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^2: Search for consecutive flush left curly braces
by ExReg (Priest) on Jun 10, 2015 at 16:36 UTC
    I guess I was unclear in my original post. I am dealing with multiple lines slurped up into a single string. The curlies I am looking for will have \n embedded in the string before them, so I have to use either \n or ^ in the expression along with s and m modifiers.