This is similar to Wiggins, except it checks to make sure that

        A. We have a two word sequence
        B. We don't have a comma

These are still very basic levels of processing, though - Wiggins is right that we 
would need to see a more thorough example of data ranges to know exactly what to 
offer. Tested, the results of this were that the program printed "SD" and exited as 
expected. Case sensitivity/insensitivity matching isn't required since \w will match 
either upper or lower case characters.

my $name = "SARA DEILY";
if (($name =~ /\w+\s+\w+/) and ($name !~ /,/)) {
  chomp($name);
  if ($name =~ /^(\w{1})\w*\s+(\w{1})\w*$/) {
   my $initials = join ("", $1 . $2);
   print $initials . "\n";
  }
  else {
   print "Something\'s wrong here: $!\n";
  }
}
else {
  # do more processing on different types of entries
  print "Doh!";
  exit;
}


-----Original Message-----
From: Wiggins d'Anconia [mailto:[EMAIL PROTECTED]
Sent: Sunday, July 20, 2003 7:10 PM
To: Sara
Cc: org
Subject: Re: Regex problem


  Sara wrote:
> $name = "SARA DEILEY";
> 
> how its possible to grasp only initials for First and Last name i.e $name ="SD"??
> 

Depends on how standardized your data is, something simple like this 
should work for the above:

my $name = 'SARA DEILEY';
my $initials;
if ($name =~ /^(\w)\w*\s+(\w)\w*/) {
     $initials = $1 . $2;
     print "Initials: $initials\n";
}
else {
     die "Can't determine initials: $name";
}

But this presents a number of problems, for instance people with 2 
"first names", and you will have to decide what to do with names like 
McDonald, or if your data can be entered by humans, what if someone 
enters:  "Deiley, Sara", etc.

You might also consider using 'split' and 'substr' instead of using a regex.

Either way you need to provide more info about your possible data set 
range if the above is not sufficient.

http://danconia.org


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

Reply via email to