CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Help regarding regex

 



Jeff29
Novice

Dec 14, 2010, 2:57 PM

Post #1 of 11 (6792 views)
Help regarding regex Can't Post

1.My regular expression below is not matching for all the list of inputs,it fails in the first example provided below,how to make the "#\d+," optional in my regex so that it matches only if it is present..also is there a way I change .* to be more specific?
2.Also I keep getting the below warning at line "print "$path$ver\n";"
Use of uninitialized value in concatenation (.) or string at orphan_overwrite.pl line 40, <IN>

Quote
INPUT:-
... ... branch from //source/tools/perl/mm/audio/dec/rel/3.2/src/file.c#6
... ... branch from //source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#1,#8



Quote
OUTPUT:-
//source/tools/perl/mm/audio/dec/rel/3.2/src/file.c#6
//source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#8


Code
($path, $ver) = $update_p4filelog =~ /branch from (.*\/.*)#\d+,(#\d+)$/; 
print "$path$ver\n";



FishMonger
Veteran / Moderator

Dec 14, 2010, 4:22 PM

Post #2 of 11 (6785 views)
Re: [Jeff29] Help regarding regex [In reply to] Can't Post

Without seeing an example of complete lines to match against, I can't say for sure but off hand I'd probably use split to separate the fields and adjust the desired fields as needed.

When there are multiple number sequences at the end, which one do you want?


Jeff29
Novice

Dec 14, 2010, 5:19 PM

Post #3 of 11 (6781 views)
Re: [FishMonger] Help regarding regex [In reply to] Can't Post

Hi fishmonger,

I gave a sample input and desired output,that can be used to match against.When there are multiple sequences at the end (there will always be only two),I want the last one.

Thanks


FishMonger
Veteran / Moderator

Dec 14, 2010, 5:36 PM

Post #4 of 11 (6780 views)
Re: [Jeff29] Help regarding regex [In reply to] Can't Post

The '... ...' portion of your example leads me to believe that there is more data on the line than you posted.


Code
#!/usr/local/bin/perl 

use strict;
use warnings;
use Perl::Tidy;
use 5.10.0;
use Data::Dumper;

while ( my $update_p4filelog = <DATA> ) {
chomp $update_p4filelog;
my @parts = split(/#/, $update_p4filelog);

my $ver = $parts[-1];
my $path = (split(/ /, $parts[0]))[-1];

say Dumper ($path, $ver);
}

__DATA__
... ... branch from //source/tools/perl/mm/audio/dec/rel/3.2/src/file.c#6
... ... branch from //source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#1,#8



FishMonger
Veteran / Moderator

Dec 14, 2010, 5:49 PM

Post #5 of 11 (6776 views)
Re: [Jeff29] Help regarding regex [In reply to] Can't Post

Or, with a little modification to your regex.


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



Jeff29
Novice

Dec 14, 2010, 6:13 PM

Post #6 of 11 (6773 views)
Re: [FishMonger] Help regarding regex [In reply to] Can't Post

Yes,actually there is more data before,there are couple of lines before ... ... branch from,does that make a difference?
I keep getting the following message and incorrect output when I use the below regex

Code
~ /branch from (.*\/.*)(?=#\d+,)?#(\d+)$/;"


Code
Quantifier unexpected on zero-length expression before HERE mark in regex m/branch from (.*/.*)(?=#\d+,)?#(\d+)$ << HERE / at perl.pl



(This post was edited by Jeff29 on Dec 14, 2010, 6:14 PM)


FishMonger
Veteran / Moderator

Dec 14, 2010, 6:28 PM

Post #7 of 11 (6765 views)
Re: [Jeff29] Help regarding regex [In reply to] Can't Post

Please post a short but complete example script that demonstrates that error.

Here's my successful test script based on your posted sample data.


Code
#!/usr/local/bin/perl 

use strict;
use warnings;
use 5.10.0;
use Data::Dumper;

while ( my $update_p4filelog = <DATA> ) {
chomp $update_p4filelog;
my ($path, $ver) = $update_p4filelog =~ /branch from (.*\/.*)(?=#\d+,)?#(\d+)$/;

say Dumper ($ver, $path);
}

__DATA__
... ... branch from //source/tools/perl/mm/audio/dec/rel/3.2/src/file.c#6
... ... branch from //source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#1,#8


Outputs:

Quote
D:\perl>perl-2.pl
$VAR1 = '6';
$VAR2 = '//source/tools/perl/mm/audio/dec/rel/3.2/src/file.c';

$VAR1 = '8';
$VAR2 = '//source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#1,';



Jeff29
Novice

Dec 14, 2010, 6:37 PM

Post #8 of 11 (6761 views)
Re: [FishMonger] Help regarding regex [In reply to] Can't Post

Hi Fishmonger,

The only difference is the input and I am using perl 5.6.1

$update_p4filelog contains the following data,your code fails with this input,actually I created a sample script below,it gives the same warning message even with the earlier stated input.

//depot/asic/msmshared/users/QTVOEMDrops/7600/3555473R/qtv/player/audioplayer/audiocmx.cpp
... #2 change 1548905 edit on 2010/12/09 by c_krisri@C_KRISRIXP (ktext) 'Audio loss when doing nultiple '
... #1 change 1548876 branch on 2010/12/09 by c_krisri@C_KRISRIXP (ktext) 'Branching for orphan release fo'
... ... branch from //source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#1,#8

Code
#!/usr/bin/perl 
use strict;
use warnings;
use Data::Dumper;
my $update_p4filelog;
$update_p4filelog="... ... branch from //source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#1,#8";
print "$update_p4filelog\n";
my $update_base_branched_p4path = $update_p4filelog = ~ /branch from (.*\/.*)(?=#\d+,)?#(\d+)$/;
print "$update_base_branched_p4path\n";



(This post was edited by Jeff29 on Dec 14, 2010, 6:49 PM)


FishMonger
Veteran / Moderator

Dec 14, 2010, 7:10 PM

Post #9 of 11 (6748 views)
Re: [Jeff29] Help regarding regex [In reply to] Can't Post


Quote
The only difference is the input and I am using perl 5.6.1


Those 2 facts are the reason for the failure.

That's a very old/outdated version. There is absolutely no valid reason to use any thing prior to 5.8.8, most would say that 5.10.0 is the minimum/oldest version that you should be using.


Code
#!/usr/local/bin/perl 

use strict;
use warnings;
use Data::Dumper;

while ( my $update_p4filelog = <DATA> ) {
chomp $update_p4filelog;
if ( $update_p4filelog =~ /branch from (.*\/.*)(?=#\d+,)?#(\d+)$/ ) {
my ($path, $ver) = ($1, $2);
print Dumper ($ver, $path);
}
}

__DATA__
//depot/asic/msmshared/users/QTVOEMDrops/7600/3555473R/qtv/player/audioplayer/audiocmx.cpp
... #2 change 1548905 edit on 2010/12/09 by c_krisri@C_KRISRIXP (ktext) 'Audio loss when doing nultiple '
... #1 change 1548876 branch on 2010/12/09 by c_krisri@C_KRISRIXP (ktext) 'Branching for orphan release fo'
... ... branch from //source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#1,#8


output of D:\perl>perl-2.pl

Quote
$VAR1 = '8';
$VAR2 = '//source/tools/perl/mm/player/nov/rel/1.1/src/file.cpp#1,';



Jeff29
Novice

Dec 14, 2010, 7:31 PM

Post #10 of 11 (6744 views)
Re: [FishMonger] Help regarding regex [In reply to] Can't Post


Quote
I need to get this working with 5.6.1 only,is there a way I change the regex to work for this version?



Jeff29
Novice

Dec 14, 2010, 7:50 PM

Post #11 of 11 (6740 views)
Re: [Jeff29] Help regarding regex [In reply to] Can't Post

I figured it out...,the following works for me

Code
~ /branch from (.*\/.*?)(?:#\d+,)?(#\d+)$/;



(This post was edited by Jeff29 on Dec 14, 2010, 7:50 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives