in reply to how to retrieve data between <script></script> and save to an array

I would like to write a perl script to ...

Are you sure? Have you read perlintro?

Lets see if I can avoid writing stuff :)

between < </ -> Require everything between innermost tags to compile.

Aww, off target, but a new search term emerges, tags. New try between tags -> Remove text between two Start and End Tags (Regex), Accessing data between two tags, Extracting text between HTML comments tags, Regular expression to match text between two tags (was: Help!! Regular Expressions)

That looks better, now for some copy/paste

my $left = quotemeta '<fun>'; my $right = quotemeta '</nuf>'; while( $line =~ /$left(.*?)$right/i ){ my $between = $1; }

Replies are listed 'Best First'.
Re^2: how to retrieve data between <script></script> and save to an array
by curd8341 (Initiate) on Apr 10, 2012 at 07:58 UTC

    Hi, there, the one you posted following won't work unfortunately

    my $left = quotemeta '<fun>'; my $right = quotemeta '</nuf>'; while( $line =~ /$left(.*?)$right/i ){ my $between = $1; }

    in my string, there are mixed one line and multiple line between <script> and </script>, how to use regular expression to handle both cases ? Thanks, curd

      Sure it will, just add /s , as in m//si

        thanks for the info, Actually I need to add /g besides smi. Here is the script and it works for the string I provided. Here is the code

        my @script=(); open FILE, "content" or die "Couldn't open file: $!"; while (<FILE>){ $line .= $_; } close FILE; my $left = quotemeta '<script>'; my $right = quotemeta '</script>'; while( $line =~ /$left(.*?)$right/gsmi ){ push (@script, $1); }

        But it will break if $line contains html page with other tags and following <script> contents, for example:

        <html> <head> <script language="javascript" src="" id="scptMDtl"></script> <script language="javascript">var onWL=0;var oJS = new Object();</scri +pt> <script language="javascript"> var a_fInDelItms = 0; var a_fPrv = 1; var a_fTxt = 0; </script> </head> <body> <textarea id="txtBdy" class="w100 txtBdy" style="display:none"> This is a test. </textarea> <script language="javascript"> var a_sId = "RgAAAADz6rvTg9+xRIGTnTWlmKbHBwCeJ0LFThYQQaU\/Nvmo +3DG+AAACNa1IAABO5K\/6RD2SRbSFY4IMoQCbAAAAFHnvAAAJ"; var a_sCK = "TuSv+kQ9kkW0hWOCDKEAmwAAABXdNQ=="; </script> </body> </html>

        could you please to see how to that by giving this long string, Thanks,