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

Dear Gods

I have the following example of a text log file:
Samanchi JAM 3.5 FW [**REQUIRES USER CONTEXT**] IM CBAS 4.0 IM-Global [**REQUIRES USER CONTEXT**] Microsoft Office 2003 BU Configuration (IM-NA-02) Microsoft Office XP Configuration (IM-01) IM Rappa for Investment Management Russell Performance Universes Fonts 2.1 IMG [**REQUIRES USER CONTEXT +**] IM CBAS PowerPointAddin 3.8a IM Global [**REQUIRES USER CONTEXT**] IM CBAS Prod (WinDist) [**REQUIRES USER CONTEXT**] LineData Services LongView 2000 2.5.5 MSIM-NY IM CBAS 4.0 IM-Global IM GSD Prod (WinDist) [**REQUIRES USER CONTEXT**] Microsoft Office 2003 BU Configuration (IM-NA-01) [**REQUIRES USER C +ONTEXT**] Russell Performance Universes Fonts 2.1 IMG
I want to capture every line that finishes off with [**REQUIRES USER CONTEXT**]. So I wrote this bit of code:
print "$1\n" if $item =~ /(.+)\[\*+REQUIRES USER CONTEXT\*+\]$/;
However, I get no output! The above does make sense to me, but obviously its not correct.

Can you kindly enlighten me please on the reason why I get no output, and how I can remediate it?

Thanks.
Blackadder

Replies are listed 'Best First'.
Re: Traping [** using reg exp
by jeffa (Bishop) on Sep 08, 2004 at 14:56 UTC

    Hmmm ... it worked for me ...

    while (<DATA>) { print "$1\n" if /(.+)\[\*+REQUIRES USER CONTEXT\*+\]$/; } __DATA__ Samanchi JAM 3.5 FW [**REQUIRES USER CONTEXT**] IM CBAS 4.0 IM-Global [**REQUIRES USER CONTEXT**] Microsoft Office 2003 BU Configuration (IM-NA-02) Microsoft Office XP Configuration (IM-01) IM Rappa for Investment Management Russell Performance Universes Fonts 2.1 IMG [**REQUIRES USER CONTEXT +**] IM CBAS PowerPointAddin 3.8a IM Global [**REQUIRES USER CONTEXT**] IM CBAS Prod (WinDist) [**REQUIRES USER CONTEXT**] LineData Services LongView 2000 2.5.5 MSIM-NY IM CBAS 4.0 IM-Global IM GSD Prod (WinDist) [**REQUIRES USER CONTEXT**] Microsoft Office 2003 BU Configuration (IM-NA-01) [**REQUIRES USER C +ONTEXT**] Russell Performance Universes Fonts 2.1 IMG
    Are you sure that $item contains the line of text in question?

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Traping [** using reg exp
by mutated (Monk) on Sep 08, 2004 at 15:29 UTC
    Does removing the $ at the end of your regexp fix the problem? I'm assuming that given the content of the file you are parsing you are running this script in windows? Is there a \r character at the end of the line? Hint try chop($item) does it remove the ]? if not then you definately have some characters hanging around after the ] that you can't see...Droping the anchor all together will give you the most flexibility as long as the text [**REQUIRES USER CONTEXT**] can never show up anywhere but at the end.


    daN.
      Adding \s befor $ at the end of the line did the trick

      This is the full script
      use strict; open (LST, "c:\\tester.txt")||die "$^E : $!\n"; chomp (my @data=<LST>); for (@data) { print "$1\n" if ($_ =~ /(.+)\[\*+REQUIRES USER CONTEXT\*+\]\s*$/); } close (LST)
      Obviously there were extra chars at the end of the line that I couldn't see.

      Thanks very much.
Re: Traping [** using reg exp
by VSarkiss (Monsignor) on Sep 08, 2004 at 15:03 UTC

    If it's a fixed string, don't bother with a regex at all. Just use substr:

    my $target = '[**REQUIRES USER CONTEXT**]'; my $len = length $target; # or just count it yourself ;-) # later... print if substr($item, -$len, $len) eq $target;
    Which depends on the fact that a negative second argument to substr counts from the end of the string.

    HTH

      This is very good alternate method if need be - it did cross my mind - but I'll carry on with the reg exp for now,....Thanks for this...Regards
      Blackadder
Re: Traping [** using reg exp
by davido (Cardinal) on Sep 08, 2004 at 15:07 UTC

    Is it possible that your text actually looks like this:

    IM GSD Prod (Windist) [** REQUIRES USER CONTEXT **] ^---Space characters--^

    Because the regex actually did work for me. Also, it wouldn't hurt to anchor the bigging of the regexp to the beginning of the line with m/^(.+)...... Probably unnecessary, but I like being explicit within regexps whenever possible.


    Dave

      Or maybe there's whitespace at the end of the line? Try changing /...$/ to /...\s*$/ (or omit the $ completely).
      Yours probably worked because that is the only line you have. His data ends with a line that doesn't ends with [**REQUIRES USER CONTEXT**]
Re: Traping [** using reg exp
by Anonymous Monk on Sep 08, 2004 at 15:02 UTC
    First of all, what exactly is $item?

    If it is one line of the text (as in Blacadder code) then it will work fine, if it isn't, then it won't work fine.

    If it contains more then one line, then you probalby want to make it a multi line regex, by adding the m modifier.

    Also, I would suggest you read about 'Greedy' and/or 'Maximal Match' in the perlre pod and/or perlretut pod

      replace Bladadder code in the previous post with jeffa's code...

      I guess it's time that I create an account so I can update my posts...