Click here to get back home

RegEx Help, Please? (match after n)

 HomeNewsGroups | Search

get this group's latest topics as an RSS feed  comp.lang.perl.misc - PERL programming language

please rate
this thread
If you were  Registered and logged in, you could reply and use other advanced thread options
Posted by Smarta55 Chris on June 26, 2005, 10:49 pm


Can someone help me find the last n folders in a path,
or the last n OUs in a fully qualified Netware name... for an app object,
etc?

Example:
\server\volume\share\folder\folder\folder
I want to grab just the last 2 folders (including or excluding the \s), but
sometimes there
are 3 folders after the share, like in the example, and sometimes there are
more.

Same for an app object in eDirectory
..appobject.dept.biz.site.county.state.tree
How to I grab just the .site.county.state.tree OUs?
Sometimes there are 5 OUs after the app object, and sometimes more.
I want, I think, to "start at the end of the string, and grab everything
after the 4th dot from the end," but don't have a clue how to code this.

I'm not a perl newbie, but I'm no expert....and I AM a RegEx idiot!

Here's some of what I've tried:
$string = ".appobject.dept.biz.site.county.state.tree";
$string =~ /\..+\..+\..+/$;    # i.e. match a literal dot followed my
anything mult times, followed by another literal dot, followed, etc...
                                        # starting at the end of the string
$string =~ /(\.\W)*/;    # \w does NOT match AlphaNum like it's supposed to,
\W DOES, but it's NOT supposed to.
                                # unless . is an AlphaNum character, but I'm
not sure

And I can't remember the 4,612 other patterns I've tried, or the
accompanying 4,612 hairs I've
yanked from my scalp in the process.  :-(

Thank you,
Chris




Posted by A. Sinan Unur on June 27, 2005, 3:38 am






You are missing an 'I' there ... Seriously, though, I don't know what
you are referring to.



OK.



perldoc perlfunc
perldoc -f reverse
perldoc -f split

In addition,

perldoc -f rindex
perldoc -f substr

might also be of interest, in case you want to write a different
algorithm.



Probably not, but you need to look at this calmly.



This looks like Def Poetry to me.
 


Here are two examples to get you started:

First:

#!/usr/bin/perl

use strict;
use warnings;

sub last_n_components {
    my ($s, $d, $n) = @_;
    join($d, reverse((reverse split /\Q$d\E/, $s) [0 .. $n - 1]))
}

print +last_n_components(
    q,
    '.', 2
)."\n";

print +last_n_components(
    q,
    '\', 2
)."\n";

__END__

Now, if you are going to deal with paths, your life will be much easier
if you do it portably from the start:

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec::Functions qw'catdir splitdir';

sub last_n_path_components {
    my ($s, $n) = @_;
    catdir reverse((reverse splitdir $s)[0 .. $n - 1]);
}

print +last_n_path_components(
    q, 2)."\n";

__END__


--
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


Posted by Smarta55 Chris on June 26, 2005, 11:57 pm


Thank you....for teaching me that I am, afterall, a perl newbie.
I'm more confused than before.  I'll check the perldocs you
mention, then assign this script to someone else at work.  :-(

There's no simple, single-line $ao =~ /some pattern or other/; that'll work?
I just want to append an app object's name to a biz name, then to the last 4
OUs of the match, and create
it in eDirectory with a 3rd party utility.  :-(

Thanks for your help!
Chris






Posted by A. Sinan Unur on June 27, 2005, 4:06 am






You are welcome. However, please note that top-posting and full-quoting
are generally not useful in facilitating a productive exchange.

Please do read the posting guidelines for this group. They contain
valuable information on how you can help yourself, and help others help
you.







Well, to be honest, my double reverse was very convoluted, and I have
onlyself (no, actually, HBO) to blame.
 


There is no reason to bring out the big guns for this kind of thing.
Besides, I would probably mess that up even worse than I messed up with
the double reverse.

Sinan
--
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


Posted by Smarta55 Chris on June 27, 2005, 12:19 am





Now I'm a newsgroups newbie, too!  :-)

Thanks





Subject Author Date
RegEx Help, Please? (match after n) Smarta55 Chris 06-26-2005
If you were  Registered and logged in, you could reply and use other advanced thread options
  • Subject
  • Date

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Driving a better car - Fuelzilla.com

Cabling site for homeowners and pros alike - Cabling-Design.com

1-Script XML SitemapXML Sitemap
Privacy Policy