See the following script :
#!/usr/bin/env perl
use strict;
use warnings;
my $string = "65378161_12011_Q.pdf";
if($string =~ m/((.*)?Q\.pdf)/i ){
my $inside=$2;
print " file name:$inside \n";
}
Your code just lack the '?' character to tell the regex to be not greedy.
Another way is to match all of the characters that is not a 'Q' before itself :
m/(^[^Q]+)?Q\.pdf/i
Edit:
Because you had edited your post with a different spec :
If you need to parse HTML, I recommend to use a proper module :
Don't parse or modify html with regular expressions! See one of
HTML::Parser's subclasses: HTML::TokeParser, HTML::TokeParser::Simple,
HTML::TreeBuilder(::Xpath)?, HTML::TableExtract etc. If your response
begins "that's overkill. i only want to..." you are wrong.
http://en.wikipedia.org/wiki/Chomsky_hierarchy and
here for why not to use regex on HTML
(This is a reminder about using regex to parse HTML from #perl channel on irc.freenode.org)
Edit 2:
Here a complete working example :
#!/usr/bin/env perl
use strict;
use warnings;
use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new_from_content('
<LI>
<A
HREF="65378161_12011_Q.pdf">
65378161_12011_Q.pdf
</A>
');
$tree->look_down("_tag", "a")->as_text =~ m/(^[^Q]+)Q\.pdf/i && print "$1\n";