in reply to Counting frequency of expressions in a string

You need the g (global) modifier and possibly the s (ignore line ends) modifier. You also need to remove the $ (end of pattern match). You also need to match "Perl Party".

Test code would look something like this:

use strict; use warnings; my @dates; my $string = do {$/ = ""; <DATA>}; while ($string =~ /\b(\d{2})-(\d{2})-(\d{4})\sPerl\sParty/g) { push @dates, "$1\.$2\.$3"; } print join "\n", @dates; __DATA__ All kinds of text 01-01-2003 Perl Party more text 01-01-2004 Perl Part +y and even more text 01-01-2005 Perl Party and finally some other date 01-01-2006
Update: /s removed and relevant comment struck. See bart's reply below.

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: Counting frequency of expressions in a string
by bart (Canon) on Aug 29, 2005 at 10:25 UTC
     /\b(\d{2})-(\d{2})-(\d{4})\sPerl\sParty/gs
    You don't have any dots in this regexp, so the /s is useless. You might feel like I'm nitpicking, and I am: I wouldn't mind its presence in actual production code.

    But this is a site for earning learning, you shouldn't set a bad example, too many people are cargo culting code (especially regular expressions) from this site already, so I think we should releave the code we post here from any voodoo as much as possible, so people will actually begin to understand the code they're copying.

    Thank you.

      For earning? I have not earned much here. Maybe you mean learning? ;-)


      holli, /regexed monk/

      You are of course quite right. The hidden story is that there was a .*? in there following a \G. I realised the anchor and wild card were redundant and removed them, but forgot to remove the /s.

      So the further lesson is: look, then look again. And each time you make a change, look twice more. That time I only looked once more. :)


      Perl is Huffman encoded by design.