ok..here we go with this ..
#!/usr/bin/perl
use strict;
use re 'eval';
my $Jscript = join (" ",<DATA>);
# Extract function foo from Jscript
my $content = _extract("foo",$Jscript);
sub _extract {
my ($funct,$Jscript) = @_;
#regexes to match all but starting positions of strings and commen
+ts
my $double_quote_str = qr!(\\.|[^"\\])*"!;
my $single_quote_str = qr!(\\.|[^'\\])*'!;
my $comment1 = qr![^\n\r]*!;
my $comment2 = qr!.*?\*/!s;
#now match complete strings or comments..whatever matches first wi
+ll 'eat' others
my $escape_strings = qr!
(
'(?{$start='sing_quot'})|
"(?{$start='dbl_quot'})|
//(?{$start='comment1'})|
/\*(?{$start='comment2'})
)
(?(?{$start eq 'sing_quot'})$single_quote_str)
(?(?{$start eq 'dbl_quot'})$double_quote_str)
(?(?{$start eq 'comment1'})$comment1)
(?(?{$start eq 'comment2'})$comment2)
!x;
my ($round,$curly);
# match balanced nested round braces ..escape strings and comments
$round = qr!
\((
$escape_strings
|
[^()]|(??{$round})
)*
\)
!x;
# match balanced nested curly braces ..escape strings and comments
$curly = qr!
{ (
$escape_strings
|
[^{}]|(??{$curly})
)*
}
!x;
if ($Jscript =~ m!(\bfunction\s+$funct\s*$round\s*$curly)!) {
print "Matched :\n",$1;
}
}
__DATA__
//.. function foo goes here .......
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|