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

Hi, I am reading files and selecting lines to be added to one file from other files. I am looking for lines that look like

'# Version: 1.11' or

'* Version: 1.11' or

'Version: 1.11' or

'/* Version: 1.11'

The problem is simple: I cant seem to formulate how to find that combination of /*. This is how all other lines are found, I have tried adding \/\* and so on to if statement, without success. How can I formulate search string to cover for /*-lines, too?

if( ($row =~/^[#;*\s+]?\s*version:?\s+(\d+\.\d+|)/i)
I would appreciate if anyone has idea for this

BR Hewarn

Replies are listed 'Best First'.
Re: Comment mark
by Bilbo (Pilgrim) on Feb 12, 2003 at 11:28 UTC

    This works:

    #!/usr/bin/perl -w use strict; my @data = ('# Version: 1.11', '* Version: 1.11', 'Version: 1.11', '/* Version: 1.11'); foreach my $row (@data) { if( $row =~ m/^([\#\*] | \/\* )? \s*version:?\s+ (\d+\.\d+|)/ix) { print "$row \t $2 \n"; } }

    and outputs:

    # Version: 1.11          1.11 
    * Version: 1.11          1.11 
    Version: 1.11            1.11 
    /* Version: 1.11         1.11 
    

    The regexp looks for lines whixh start either with # or * or with /* or none of these.

Re: Comment mark
by broquaint (Abbot) on Feb 12, 2003 at 11:28 UTC
    Alternation is your friend
    my @comments = ( '# Version: 1.11', '* Version: 1.11', 'Version: 1.11', '/* Version: 1.11', ); foreach(@comments) { print m<^ (?: \# | /? \* )? \s* version: \s+ (\d+ (?:\.\d+)+ ) >ix,$/; } __output__ 1.11 1.11 1.11 1.11
    See. perlre for more info.
    HTH

    _________
    broquaint

Re: Comment mark
by bronto (Priest) on Feb 12, 2003 at 11:30 UTC

    \/\*is right, but don't put it in the character class ([...]) because it will catch / and * separately. Put it outside, for example something like:

    # *** UNTESTED *** if( ($row =~/^(?:[#;*\s+]|\/\*)?\s*version:?\s+(\d+\.\d+|)/i) # *** UNTESTED ***

    Ciao!
    --bronto


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz
Re: Comment mark
by physi (Friar) on Feb 12, 2003 at 11:28 UTC
    use strict; my @row = ('/* Version: 1.11', '# Version: 1.11' , '* Version: 1.11' , + 'Version: 1.11'); for (@row) { print "$_ -> matches\n" if( ($_ =~/^([#;*\s+]|\/\*)?\s*version:?\s+(\d ++\.\d+|)/i)); }
    -----------------------------------
    --the good, the bad and the physi--
    -----------------------------------
    
Re: Comment mark
by Mask (Pilgrim) on Feb 12, 2003 at 11:26 UTC
    I think the right combination for "/*" should be something like "\/\*"