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

Hello, I am new to PERL and I am trying to implement a simple pattern matching script for our monitoring solution. Basically I receive some events and the nodename is listed at "hostname.ms.com". I need the nodename to be "hostname" with the ".ms.com" stripped off.

Someone mentioned a solution in the chatterbox section of this website but the solution scrolled off before I could write it down. Any pointers would be very helpful. Thank you.

Replies are listed 'Best First'.
Re: pattern matching question
by kennethk (Abbot) on Jul 19, 2011 at 18:27 UTC
    Some quick and dirty solutions for grabbing it might use a regular expression:

    my ($value) = $url =~ /([^.]*)/;

    or split:

    my ($value) = split /\./, $url;

    For more complex manipulations, you may consider using URI.

      Also Q&D:

      >perl -wMstrict -le "my $s = 'hostname.ms.com'; $s =~ s{ [.] .* \z }''xms; print qq{'$s'}; " 'hostname'
        Thank you AnomalousMonk (Vicar).

        A few follow up questions. what is "-wMstrict -le" mean?

        Also, can you explain this line a bit more? $s =~ s{ . .* \z }''xms; what does the \z and xms mean?

      thank you Kennethk. A follow up question. I am reading that the ^. is a metacharacter to indicated beginning of the string. So is if the variable is:

      hostname.ms.com

      is the ^. ignoring the "hostname" part of the string and just picking up the " .ms.com "? Also, why does the ^. have to be in brackets?

        On the first position inside square brackets, ^ has a different meaning: it is a negation. So, [^.] means anything but a dot.

      Hello Everyone, Thank you for your help. I think I found a solution. It may not be as pretty as the ones supplied (I am still researching what those special characters mean) but I think it will work. If anyone see's a flaw in this code, please let me know as I will be implementing into my production environment very soon. Thanks, Jaime

      #my $hostname = 'name.ms.com';

      my $hostname = 'name';

      print $hostname;

      if ($hostname =~ /\.ms.com$/) {

      print "\nthis is true";

      @nameparts = split(/\./,$hostname);

      print "\n";

      print $nameparts[0];

      print "\n";

      print $nameparts1;

      print "\n";

      print $nameparts2;

      $firstpart = $nameparts[0];

      print "\n";

      print $firstpart;

      }

      else {

      print "\nthis is false";

      }

        As I said above, please wrap code in <code> tags -- see Markup in the Monastery. Note that your array indices got linkified because you did not.

        While there are multiple ways in which code I might write differs from the above, I would point out that /\.ms.com$/ misses an escape - you probably mean /\.ms\.com$/. An easy way to avoid missing escapes is to use \Q...\E - /\Q.ms.com\E$/.

Re: pattern matching question
by Tanktalus (Canon) on Jul 19, 2011 at 22:27 UTC

    Here's another hint: when stuff scrolls off the CB, check last hour of cb (which was suggested multiple times in the CB) or the link at cb60 (currently http://mini-cb60.datenzoo.de/, but it has changed at least once). The former updates every 5 minutes (if there is any activity), but can go back more than an hour. The latter is more dynamic, but I think stuff scrolls off after an hour no matter what. It's too late now, but keep it in mind for next time. You may want to bookmark one or both of these links so you don't lose them.