我需要为字符串编写正则表达式以仅匹配 [a-zA-z0-9-._,\s] 字符集。除了提到的字符集之外,它应该会引发错误。此外,字符串的长度只能是 30 个字符。我也实现了自动换行。但是,如果字符串不是提到的字符集,我将无法抛出错误。任何帮助将非常感激!
我的字符串看起来像:
shubhwork1234 567$#@!%^&*()<>:"-,._abadcew
我的代码应该抛出一个错误 - 说明只允许 [a-zA-z0-9-._,\s] 字符集。
如果我的字符串低于 : 那么应该没有错误。
shubhworkwwwwwwwww1234567-,._ abadcew
我的代码看起来像:
#!/usr/bin/perl
my $prodname;
my $temp_str;
my $space = ' '; # word wrap done usign space character
$prodname = "shubhwork1234 567$#@!%^&*()<>:"-,._abadcew";
print (Product name not specified-name 1\n) unless($prodname);
print "\n Product name is : $prodname";
# For Product name , only 30 characters are allowed.
print "\nLength of product name : ",length($prodname),"\n";
if(length($prodname) > 30)
{
print "\n Hello world";
$temp_str = substr($prodname,0,40);
print qq| Length of Product Name can be 40 characters.Terminating rest of the string\n|);
#Handling xml special characters >,<,&,",'
$temp_str =~ s/&/&/g;
$temp_str =~ s/</</g;
$temp_str =~ s/>/>/g;
$temp_str =~ s/"/"/g;
$temp_str =~ s/'/'/g;
$temp_str =~ s/[\x00-\x08\x0B\x0C\x0E-\x19]//g;
# Word wrap
my $rindx = rindex($temp_str,$space);
$temp_str = substr($temp_str,0,$rindx);
print "\n Sting temp is : $temp_str";
#Here I ma not able to use negate properly for the character set.
if ($temp_str =~ m/^[a-zA-Z0-9]-._,\s*/)
{
print (qq| For product name : Invalid Input : Only [a-zA-Z0-9-._,] and spaces are allowed\n|);
}
$prodname = $temp_str;
print "\n assigning string is :",$prodname;
}