Regex: Selectively choose result group?

P

Prab_kar

Hi all,
I wonder if this can be done through regex matching.

I have a string of unix path, which can be either
/home/MyAccount/Proj/SubProj or
/home/MyAccount/Proj
From this I want to find
1. the "Proj" and "SubProj" portion, when the string has a SubProj

2. Just the "Proj" protion if there are no SubProjs.

I tried,
-------------------------------------
#!/usr/local/bin/perl -w

use strict ;
my $string = '/home/MyAccount/Proj/SubProj' ;
# my $string = '/home/MyAccount/Proj' ;

if ( $string =~ m/(.*)\/(.*)\/(.*)$/ )
{
print "Project: $2 \t SubProject: $3\n" ;
}
-------------------------------------

It works fine when the string has Proj and SubProj but, when I
uncomment the later definition of string, I get MyAccount as my
project. Is there a way I can choose Project to $2 or $3 based on the
string?

Or, maybe I'm thinking of a wrong solution.

Could anyone give me some pointers on how to "get just the proj portion
when subproj is absent" from regex.( I dont really want to use splits
and substr.)

Thanks for your time,
Prabh
 
G

Gunnar Hjalmarsson

I wonder if this can be done through regex matching.

I have a string of unix path, which can be either
/home/MyAccount/Proj/SubProj or
/home/MyAccount/Proj

1. the "Proj" and "SubProj" portion, when the string has a SubProj

2. Just the "Proj" protion if there are no SubProjs.

I tried,
-------------------------------------
#!/usr/local/bin/perl -w

use strict ;
my $string = '/home/MyAccount/Proj/SubProj' ;
# my $string = '/home/MyAccount/Proj' ;

if ( $string =~ m/(.*)\/(.*)\/(.*)$/ )
{
print "Project: $2 \t SubProject: $3\n" ;
}
-------------------------------------

It works fine when the string has Proj and SubProj but, when I
uncomment the later definition of string, I get MyAccount as my
project. Is there a way I can choose Project to $2 or $3 based on the
string?

Or, maybe I'm thinking of a wrong solution.

Try this:

if ( $string =~ m#/MyAccount/([^/]+)(?:/(.+))?# ) {
print "Project: $1" . ($2 ? "\tSubProject: $2" : '') . "\n" ;
}
 
A

Anno Siegel

Hi all,
I wonder if this can be done through regex matching.

I have a string of unix path, which can be either
/home/MyAccount/Proj/SubProj or
/home/MyAccount/Proj

1. the "Proj" and "SubProj" portion, when the string has a SubProj

2. Just the "Proj" protion if there are no SubProjs.

I tried,
-------------------------------------
#!/usr/local/bin/perl -w

use strict ;
my $string = '/home/MyAccount/Proj/SubProj' ;
# my $string = '/home/MyAccount/Proj' ;

if ( $string =~ m/(.*)\/(.*)\/(.*)$/ )
{
print "Project: $2 \t SubProject: $3\n" ;
}
-------------------------------------

It works fine when the string has Proj and SubProj but, when I
uncomment the later definition of string, I get MyAccount as my
project. Is there a way I can choose Project to $2 or $3 based on the
string?

Or, maybe I'm thinking of a wrong solution.

Not exactly wrong, but why insist in using a single regex to do
it all? First isolate the part of the path name that interests you:

m{^/home/MyAccount/(.*)/

That captures everything after "/home/MyAccount/". Splitting on '/' will
give you a two-element list if a subproject is present, otherwise just one
element. So:

my ( $pro, $sub_pro);
( $pro, $sub_pro) = split m{/} for $string =~ m{^/home/MyAccount/(.*)};

$pro will always be set when a project specification is found at all.
$sub_pro will be the subproject if there is one, undefined otherwise.

Anno
 
P

Prab_kar

Thanks a lot, Gunnar.
It works like a charm.

Now, I'll spend my weekend trying to figure _why_ it works :).

Thanks,
Prabh
 
P

Prab_kar

"...insist in using a single regex..."
Well, I've to confess, this is not strictly perl-related.
I've to implement this regex solution in an xml file and it has to be
in a single regex.
Since, the Perl way is the only way I can think of when working on
regex, I wanted to implement the solution
first in Perl and plug it in the xml file.

Thanks,
Prabh
 
G

Gunnar Hjalmarsson

Thanks a lot, Gunnar.
It works like a charm.

Now, I'll spend my weekend trying to figure _why_ it works :).

Good.

The reference docs

perldoc perlre

should then be your best friend. Also

perldoc perlretut

may be useful.

Feel free to come back here if there is something you fail to figure out.
 
A

Anno Siegel

Please give an attribution and some context with your reply.
"...insist in using a single regex..."
Well, I've to confess, this is not strictly perl-related.
I've to implement this regex solution in an xml file and it has to be
in a single regex.

So I spent my time working out a solution that you can't use because
you didn't tell us the whole story. Care to imagine how that sits with
me?

Anno
 
R

Rasto Levrinc

Gunnar said:
I wonder if this can be done through regex matching.

I have a string of unix path, which can be either
/home/MyAccount/Proj/SubProj or
/home/MyAccount/Proj


1. the "Proj" and "SubProj" portion, when the string has a SubProj

2. Just the "Proj" protion if there are no SubProjs.

I tried,
-------------------------------------
#!/usr/local/bin/perl -w

use strict ;
my $string = '/home/MyAccount/Proj/SubProj' ;
# my $string = '/home/MyAccount/Proj' ;

if ( $string =~ m/(.*)\/(.*)\/(.*)$/ )
{
print "Project: $2 \t SubProject: $3\n" ;
}
-------------------------------------

It works fine when the string has Proj and SubProj but, when I
uncomment the later definition of string, I get MyAccount as my
project. Is there a way I can choose Project to $2 or $3 based on the
string?

Or, maybe I'm thinking of a wrong solution.


Try this:

if ( $string =~ m#/MyAccount/([^/]+)(?:/(.+))?# ) {
print "Project: $1" . ($2 ? "\tSubProject: $2" : '') . "\n" ;
}

same thing, but shorter...

if ( $string =~ m#/MyAccount/([^/]+)/?(.*)# ) {
print "Project: $1" . ($2 ? "\tSubProject: $2" : '') . "\n" ;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top