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

Everything was working a while back until I came across two paths like below mentioned in @txt, for some reason my below program is not giving anyoutput, all I am trying to do is in the two paths provided in @txt ,I am trying to see if there is a line that contains "branch from",if there is such a match get the p4 path based on a regex(I verified this regex to be working when I pass only the "branch from"line instead of the complete path shown in @txt. Need perl monks input on why is it not matching now?I have provided the input and sample output below.

#!/usr/bin/perl use strict; use warnings; use diagnostics; my @txt=("//perl/tools/files/data/HFAT_01_01/src/VU_CORE_STORAGE_HFAT. +01.01.36.txt ... #1 change 1508970 branch on 2010/11/08 by user (ktext) 'New Featur +es: None. Fixes: Upd' ... ... branch from //perl/tools/files/data/HFAT_01_01/src/VU_CORE_STO +RAGE_HFAT.01.01.35.txt#1 ... ... branch into //perl/tools/files/data/HFAT_01_01/src/VU_CORE_STO +RAGE_HFAT.01.01.37.txt#1", "//depot/asic/msmshared/umts/wcdma/rrc_temp/temp_76xx/temp_13505D/rrcr +bcommon.c ... #1 change 1528566 add on 2010/11/23 by user (ktext) 'Added code to + avoid bufferoverf' ... ... branch from //perl/tools/files/modem/rrc/main/latest/src/rrcrb +common.c#1,#25"); foreach my $line (@txt) { print "$line\n"; if ($line =~ /branch from/) #seems like the string "branch from" is no +t matching since $1 below prints nothing { #print ">>>$1<<<\n"; my ($update_p4path, $ver) = $line =~ /branch from (.*\/.*?)(?:#\d+,)?( +#\d+)$/; print "\n$update_p4path\n"; } } #OUTPUT SHOULD BE:- #//perl/tools/files/data/HFAT_01_01/src/VU_CORE_STORAGE_HFAT.01.01.37. +txt#1 #//perl/tools/files/modem/rrc/main/latest/src/rrcrbcommon.c#25 #OUTPUT SHOULD BE:- #//perl/tools/files/data/HFAT_01_01/src/VU_CORE_STORAGE_HFAT.01.01.37. +txt#1 #//perl/tools/files/modem/rrc/main/latest/src/rrcrbcommon.c#25

Replies are listed 'Best First'.
Re: Match problem
by BrowserUk (Patriarch) on Dec 22, 2010 at 01:01 UTC
    #seems like the string "branch from" is not matching since $1 below prints nothing

    $1 refers to text matched by the first set of capturing parens (if any). Since your regex:

    if ($line =~ /branch from/)

    doesn't contain any capturing parens, $1 will never be set.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Match problem
by toolic (Bishop) on Dec 22, 2010 at 01:03 UTC
    #seems like the string "branch from" is not matching since $1 below prints nothing
    The string "branch from" is matching, and you can prove it by using some visible delimiters (Tip #3 from Basic debugging checklist) as follows:
    print ">>>$1<<<\n";#doesnt print anything

    However, $1 is undefined because you need to use capturing parentheses in your regex. You really should use strict and warnings and perltidy.

      I modified the code in my original post a little bit adding he suggestions made by you.

      I see part of my output getting printed,i.e.,"//perl/tools/files/modem +/rrc/main/latest/src/rrcrbcommon.c" is getting printed but why is "// +perl/tools/files/data/HFAT_01_01/src/VU_CORE_STORAGE_HFAT.01.01.35.tx +t#1" still not getting printed?path after the word "branch from" has +to be printed.

        I noticed the following probs

        1.Whenever there is an underscore "_" in the input the regex is failing

        2.Also after taking care of first step,meaning removing underscores and them removing the line after "branch from" ,i.e.,... ... branch into //perl/tools/files/data/HFAT_01_01/src/VU_CORE_STORAGE_HFAT.01.01.37.txt#1",the regex matches.

        Can the monks provide input on how to change the regex to match the above two scenarios?Is there a way I can just get the "branch from" line and perform my regex on that line?

Re: Regex problem
by Anonymous Monk on Dec 22, 2010 at 05:32 UTC
    If you don't take the time to understand the answers given to you, you will keep repeating your questions.