in reply to Re^5: Regex to grab data after the work "branch from"
in thread Regex to grab data after the work "branch from"

I actually want to make the "#\d+," part optional in the regex,so I changed it to [#\d+,]?,it served part of my purpose,it became optional but I want to stop it from printing.how do i do that?

($path, $ver) = $update_p4filelog =~ /branch from (.*\/.*)[#\d+,]?(#\d ++)$/;

Replies are listed 'Best First'.
Re^7: Regex to grab data after the work "branch from"
by Anonymous Monk on Dec 15, 2010 at 01:27 UTC

      I thought you were following me closely,I want "#1," to be optional,to be exact "[#\d+,]" in the regex to be optional,if it is present match it but dont print it...,if it is not present ignore it..

      what I meant was,the below code prints

      //source/qcom/qct/multimedia/qtv/player/audioplayer/rel/1.1/src/file.cpp#1,#8

      whereas I only want to print

      //source/qcom/qct/multimedia/qtv/player/audioplayer/rel/1.1/src/file.cpp#8

      my $update_p4filelog = "...xyz branch from //source/qcom/qct/multimedi +a/qtv/player/audioplayer/rel/1.1/src/file.cpp#1,#8"; my ($path, $ver) = $update_p4filelog =~ /branch from (.*\/.*)[#\d+,]?( +#\d+)$/; my $update_base_branched_p4path = "$path$ver"; print "$update_base_branched_p4path\n";
        I am legion
        So why don't you write code to do just that?
        my $update_p4filelog = "...xyz branch from //source/qcom/qct/multimedi +a/qtv/player/audioplayer/rel/1.1/src/file.cpp#1,#8"; my ($path, $ver) = $update_p4filelog =~ /branch from (.*\/.*)[#\d+,]?( +#\d+)$/; use DDS;Dump( $path, $ver ); __END__ $VAR1 = '//source/qcom/qct/multimedia/qtv/player/audioplayer/rel/1.1/s +rc/file.cpp#1,'; $VAR2 = '#8';

        You probably don't want a character class [#\d+,], but rather a (non-capturing) group: (?:#\d+,). But the real problem you're having is that the preceding .* is gobbling up the optional part. So, make it non-greedy, i.e. .*?

        for my $update_p4filelog ( "...xyz branch from //source/qcom/qct/multimedia/qtv/player/audiop +layer/rel/1.1/src/file.cpp#1,#8", "...xyz branch from //source/qcom/qct/multimedia/qtv/player/audiop +layer/rel/1.1/src/file.cpp#8", ) { my ($path, $ver) = $update_p4filelog =~ /branch from (.*\/.*?)(?:# +\d+,)?(#\d+)$/; my $update_base_branched_p4path = "$path$ver"; print "$update_base_branched_p4path\n"; } __END__ //source/qcom/qct/multimedia/qtv/player/audioplayer/rel/1.1/src/file.c +pp#8 //source/qcom/qct/multimedia/qtv/player/audioplayer/rel/1.1/src/file.c +pp#8