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

Hi there Monks,
In my code I have this:
my $title = "This is the New Title cool_image.gif Tue Feb 8 09:43:17 2 +005"; if($title =~/(.*?)(\w+|\W+\.\w+)(.*?)/ig){ print "<br><b><font color=yellow>**1=$1 *2=$2 * 3=$3 *****</b></font>< +br>"; }else{print "<br><b><font color=yellow>1316 No ok match</b></font>";}

Actually what I am trying to get, is this part out of the text:
This is the New Title, and ignore the rest, but I am having no luck, why I am not matching on the first $1?
I just need to grab any text before it finds an image name.
Thank you for the help!

Replies are listed 'Best First'.
Re: Matching Problem
by Random_Walk (Prior) on Feb 08, 2005 at 15:51 UTC

    something like this will do it if it is always a .gif that marks the break.

    my $title = "This is the New Title cool_image.gif Tue Feb 8 09:43:17 2 +005"; if ($title=~/(.*)\s+(\w+\.gif)/) { print $1 }else { print "no match" }

    or to be a bit more generic

    $title=~/(.*)\s+(\w+\.\w+)/

    there is a problem if you have two xxxx.xxx items in the title, the .* will be greedy so in that case we can use this

    $title=~/(.*?)\s+(\w+\.\w+)/

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: Matching Problem
by jdporter (Paladin) on Feb 08, 2005 at 15:27 UTC
    Well, assuming image file names contain no spaces, you could exploit the (apparent) rigidness of the date format:
    $title =~ /(.*) (\S+) (... ... \d+ ..:..:.. ....)$/
    After that, the actual title will be in $1, the image file name will be in $2, and the date string will be in $3.
Re: Matching Problem
by RazorbladeBidet (Friar) on Feb 08, 2005 at 15:46 UTC
    The grouping is a little off:
    $title =~/(.*?)((?:\w+|\W+)\.\w+)(.*?)/ig
Re: Matching Problem
by cog (Parson) on Feb 08, 2005 at 15:51 UTC
    You're not matching This is the New Title on $1, only This, because you're using a non-greedy match: (.*?).

    As soon as it gets to a \W, it stops "putting things in $1".

    Exactly what output are you looking for? Are you trying to extract everything before the date? Everything before the filename?