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

HI All,

I have pattern which looks like this:

$pattern=Latest 29 Items in Feed & 0 New Fetched items';

In the above pattern string.I would like generalised 29 and 0..

any idea how we do it??? example the patter would want to like Latest x Items in Feed & x New Fetched items';

where x means any digit

Replies are listed 'Best First'.
Re: Generalising string in pattern
by Utilitarian (Vicar) on Nov 13, 2009 at 07:52 UTC
    Your question is very unspecific, assuming you want to match this string format given varaible is called $pattern in which case you should look at the \d character class in perlre and perhaps consider using qr() if this regex is to be used repeatedly
Re: Generalising string in pattern
by bichonfrise74 (Vicar) on Nov 13, 2009 at 08:49 UTC
    Based on how I interpret your request, I think this is what you want.
    #!/usr/bin/perl use strict; my $pattern = qr/Latest \d+ Items in / . qr/Feed & \d New Fetched items/; while (<DATA>) { print if /$pattern/; } __DATA__ Latest 29 Items in Feed & 0 New Fetched items Testing Latest 28 Items in Feed & 0 New Fetched items Latest 100 Items Latest 28 Items in Feed & 0 New Fetched items
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Generalising string in pattern
by Corion (Patriarch) on Nov 13, 2009 at 08:01 UTC

    See perlre. If you show us the code you've written, we can give you more concrete comments about it.

      #!/usr/bin/perl

      $filepath="D:/fetch/autofetch.log";

      my $pattern1 =' Latest 29 Items in Feed & 0 New Fetched items';

      sysopen($FILE,$filepath,o_RDWR) or die "could find the file";

      while (my $line = <$FILE>) { # print $line if ($line =~m/\b$pattern1/ ); if($line =~m/$pattern/ ) { print $line; }

      In the file i am searching for $pattern1.

      The file has

      Latest 29 Items in Feed & 0 New Fetched items

      Latest 28 Items in Feed & 0 New Fetched items

      Latest 28 Items in Feed & 0 New Fetched items

      Now i want pattern to match all 3..

      But now my present pattern matches only first one

        See perlre about \d, or about the [...] character class notation.

        Also, you might want to use <code>...</code> tags around your code and data so it renders and downloads nicely.