in reply to Perl file name parsing - Regular expression

Hello rinkish85,

I think the following does what you want:

use strict; use warnings; use feature 'say'; my @files = ( 'log4perl.Advanced1999:06:56:12.General_Information.ErrorLog', 'log4perl.Advanced1999:06:56:12CSS.General_Information-collector-2 +.ErrorLog', 'log4perl.Advanced1999:06:56:12CSS.General_Information-collector-2 +.INVALID', ); for (@files) { if (/ ^ [\w_-]+ \. [\w:]+ \. [\w_-]+ \. ErrorLog $ /x) { s/ ^ ([\w_-]+) \. ([\w:]+) CSS \. /$1.$2./x; say; } }

Output:

13:43 >perl 1783_SoPW.pl log4perl.Advanced1999:06:56:12.General_Information.ErrorLog log4perl.Advanced1999:06:56:12.General_Information-collector-2.ErrorLo +g 13:43 >

See perlrecharclass.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Perl file name parsing - Regular expression
by rinkish85 (Novice) on May 30, 2017 at 06:28 UTC
    Thank you so much.
    for (@files) { if (/ ^ [\w_-]+ \. [\w:]+ \. [\w_-]+ \. ErrorLog $ /x) { s/ ^ ([\w_-]+) \. ([\w:]+) CSS \. /$1.$2./x; say; } }
    Can i extend  s/ ^ ([\w_-]+) \. ([\w:]+) CSS \. /$1.$2./x; subsitution of CSS to [A-Z]. ?
      Yes you can, but you need to add a quantifier. For example:
      s/ ^ ([\w_-]+) \. ([\w:]+) [A-Z]{3} \. /$1.$2./x;
      or possibly:
      s/ ^ ([\w_-]+) \. ([\w:]+) [A-Z]+ \. /$1.$2./x;
      But you might have to watch out that [A-Z]+ will not match some other uppercase letter(s) before in some other file names.