[0-9]+ means at least one digit in range 0-9.
If there is no digit, the string won't match.
You want [0-9]* instead.

--Octavian

  ----- Original Message ----- 
  From: Rick T 
  To: Perl Beginners 
  Sent: Friday, June 01, 2018 11:54 PM
  Subject: difficulty with matching


  This is a newbie question, I’m sure. But I get perplexed easily!


  The follow code segment expects to receive a $student_id consisting of a 
surname followed by a hyphen followed by a number. The die is for testing what 
I’m doing. 


  If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. But if 
I feed it ‘jones-‘ omitting the number on the end, it dies with no matches at 
all (or blanks): ‘ , , ‘ and I cannot figure out why it does not die with 
‘jones, -, ' and would appreciate an explanation. Many thanks!


      $student_id = lc $student_info_hash{student_id}; # Impose lower case
      $student_id =~ s/\s//xmsg;                       # Remove whitespace

      $student_id =~
          m{
              \A([a-z]+)  # match and capture leading alphabetics 
              (-)         # hyphen to separate surname from student number
              ([0-9]+\z)  # match and capture trailing digits
           }xms;          # Perl Best Practices
      $student_surname = $1;
      my $hyphen       = $2;
      $student_number  = $3;
  die "$student_surname, $hyphen, $student_number”;


  Rick Triplett

Reply via email to