Deb wrote:
>
> Here's some test code I'm working with:
>
> ## begin ##
>
> while ($name = <DATA>) {
> $name =~ /(\w*)\.*/;
> $name{$1}++;
> $name =~ /(\w+)/;
> print "$& \n";
> }
>
>
> __DATA__
> tibor.test.net
> mars.test.net
> moon-bx-r.test.net
> moon-bs-d.test.net
> moon-bt-321.test.net
>
> ## end ##
>
> This works for hostnames without hyphens, but when there is a hyphen in the
> name, everything after the hyphen is ignored.  I've been trying things like
> $name =~ /[a-z]*\-*\-*/ with no luck.  The data coming into the expression
> may or may not be fully qualified, so I can't just take everything to the left
> of .test.net, and the domain name may be different at times, anyway.
>
> So what I'm left with finding an expression that will match any alphanumeric,
> with 0 or more embedded dashes.  It sounds simple, but I can't seem to find
> it.
>
> What am I missing?

Hi Deb.

You're missing the hyphen from the character class. The \w class
is the same as [0-9A-Za-z_], and what you need is all of those
characters plus 'hyphen'.

This seemed a good time to showcase the much-misunderstood and
underused qr// construct. If we do this:

  my $w = qr/[\w-]/;

then there is a new character class all on its own which you can use
instead of \w in your regexes. Check out the program below.

But I'm left wondering what you're trying to do with the lines.

  $name =~ /(\w*)\.*/;
  $name =~ /(\w+)/;

which I can't fathom.

HTH,

Rob


use strict;
use warnings;

my $w = qr/[\w-]/;  # Word characters plus hyphen

my %name;

while (my $name = <DATA>) {
  $name =~ /($w*)/;
  $name{$1}++;
  print "$1\n";
}

__DATA__
tibor.test.net
mars.test.net
moon-bx-r.test.net
moon-bs-d.test.net
moon-bt-321.test.net


**OUTPUT

tibor
mars
moon-bx-r
moon-bs-d
moon-bt-321




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to