To check if a string contains only alphabets and numbers in Perl, you can use a regular expression with the =~
operator.
In this example,
=~
operator with the regular expression pattern to check if the string matches the pattern.$match_alphabets_and_numbers
will be true
, otherwise false
.$str = "Hello123";
$pattern = qr/^[a-zA-Z0-9]+$/;
$match_alphabets_and_numbers = $str =~ $pattern;
print "String contains only alphabets and numbers: $match_alphabets_and_numbers\n";
String contains only alphabets and numbers: 1
In this tutorial, we learned How to Check if String contains only Alphabets and Numbers in Perl language with well detailed examples.