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

Hi All

I used to be good at this but I am really rusty, I have a lines of text like this

\\\\serverb \\servername\...\sada\ \\\srvDC\d$\...\trsa \\name_of_server\...\
I only want to extract the server name, which is the first alphabetical char sequence after any number of slashes starting from the left-hand side, and discarding the rest.

so all I end up with is serverb, servername, srvDC and name_of_server

Thanks for your help.

Replies are listed 'Best First'.
Re: Pattern Matching.
by BrowserUk (Patriarch) on Jun 30, 2003 at 09:16 UTC

    This will probably do it.

    print $1 if $line =~ m[^ \\+ ( [^\\]+ ) ]x;

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Pattern Matching.
by PodMaster (Abbot) on Jun 30, 2003 at 09:31 UTC
    my( $bleep ) = split /\\+/, $line, 2;
    update: disregard the above. Here's an alternate pattern
    my $bleep = $1 if $line =~ /^ \\+ (.*?) (?=\\)? /x;
    on second thought, if you really wanted to use split, something like
    my $bleep = (split /\\+/, $line, 3)[-2];
    should work (and it does).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.