Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Search for consecutive flush left curly braces

by ExReg (Priest)
on Jun 10, 2015 at 14:27 UTC ( [id://1129866]=perlquestion: print w/replies, xml ) Need Help??

ExReg has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a tool to pick apart and analyze C++ files with lots of regexes. I have been slurping in the files and then splitting apart the functions by using split on \n\} . (Functions normally start with a flush left { and end with a flush left }). I found one file where there were two flush left }'s before the next flush right {. I am trying to figure out a way to search for this.

What regex can I use to search for where a \n\} is followed later by another \n\} without a \n\{ in between? I've tried to think of look ahead, look behind, both positive and negative, and can't come up with anything.

Any wisdom would be appreciated.
  • Comment on Search for consecutive flush left curly braces

Replies are listed 'Best First'.
Re: Search for consecutive flush left curly braces
by MarkusLaker (Beadle) on Jun 10, 2015 at 15:11 UTC
    Does this do what you need?
    #!/usr/bin/perl use warnings; use strict; use v5.10.0; my $rx_consecutive = qr/ ^ } (?: (?! ^ [{}] ) . )* ^ } /msx; my @strings = split /!/, <<'STRINGS'; f() { fred(); } g() { jim(); } ! f() { fred(); } int i = 0; } g() { jim(); } ! f() { if (i) { fred(); } } g() { jim(); } STRINGS for my $text (@strings) { say "Testing:\n$text\n"; my $found = $text =~ $rx_consecutive; say "Double right-curly ", ($found? "found": "not found"); }
      Thanks! Exactly what I was looking for. I am humbled by your wisdom and in awe of the beauty of your answer.
Re: Search for consecutive flush left curly braces
by stevieb (Canon) on Jun 10, 2015 at 15:27 UTC

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

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

      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 ;)

      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.
Re: Search for consecutive flush left curly braces (gcc-xml castxml)
by Anonymous Monk on Jun 11, 2015 at 03:35 UTC
Re: Search for consecutive flush left curly braces
by sundialsvc4 (Abbot) on Jun 11, 2015 at 00:19 UTC

    May I also cautiously suggest that you might be “implementing a language parser, the hard way.”   The CPAN library contains both very-powerful lexers/parsers that are written in Perl, and interfaces to well-known legacy systems.   Perhaps by using one of these tools you can dramatically change (for the better ...) your approach to this problem.   Several LALR-grammars for the C++ language, for example, are already available off-the-shelf.

      As much as I like CPAN, I cannot load any of it on to the workstations at work. I am stuck with what I have and any other needed functionality has to be created by me. At least they finally upgraded from Perl 5.005!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1129866]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (None)
    As of 2024-04-25 04:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found