Although counting curlies might succeed quite foten, it will fail if you somehwre have a string constant containing a an unbalanced curly or acomment.

public void readExternal () { ... '}' /* this } is NOT the end as well */ }

So it seems you can't get around a basic tokenizer that can differenciate code curlies from string-constant curlies. At least Java syntax is not as hard to parse as Perl, the only way to create a place where a curly does not close a block is in "" and '', after // and between /* and */, there's no qq,q,qr,qw or s///,m//,tr/// ....

A quick solution (demonstrating only basic functionaility):

use Regexp::Common; local $_ = join '',<>; my $code = ''; my $curlies = 1; #one curly open while( m# \G ( [^{}'"/]* ) #xg ){ $code .= $1; my $p = pos; # '' + "" if( m# \G ( $RE{quoted} ) #xg ){ $code .= $1; next; } pos = $p; # /* */ if( m# \G ( $RE{balanced}{-begin=>'/*'}{-end=>'*/'} ) #xg ){ $code .= $1; next; } pos = $p; #// if( m# \G ( // [^\n]* ) #xg ){ $code .= $1; next; } pos = $p; # { if( m# \G \{ #xg ){ $code .= '{'; ++ $curlies; next; } pos = $p; # } if( m# \G \} #xg ){ $code .= '}'; -- $curlies; last unless $curlies; next; } pos = $p; m# \G ( . ) #sxg or last; $code .= $1; } print "CODE\n$code\n";
--
http://fruiture.de

In reply to Re: Extract lines of .java by fruiture
in thread Extract lines of .java by knobbled

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.