If you mean that a string must have four digits at the end, that is, the four digits are preceded by a non-digit, then you can use a positive look-behind assertion to look at the character before that group.
Since we are dealing with strings, and not lines, you should use string anchors (\A and \z). Working backwards, \d{4} gives us 4 digits. To see if the character before that is a non-digit? (?<=\D) (mnemonic: before equals a non-digit). This looks at the character, but does not consume it.
Then, alphanum characters and underscore can be represented by \w. As an added bonus (or possibly not, that's your call) it will work correctly on UTF-8 characters that represent alphanumish characters.
Putting it all together, we get
sub is_valid { my $test = shift; return $test =~ /\Aaaa_\w{1,24}(?<=\D)\d{4}\z/; }
I naturally tend to wrap this logic up in a routine , so that in the mainline code I can write
for my $str (qw(aaa_gjh1_dfgdf_0009 aaa_gjh_0000 aaa_fdsfs_000 aaa_sdf +_jdsh_01111)) { print (is_valid($str) ? "ok $str\n" : "not ok $str\n"); }
• another intruder with the mooring in the heart of the Perl
In reply to Re: Number of digits at the end of the string
by grinder
in thread Number of digits at the end of the string
by isha
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |