You can take advanntage of the fixed width property with unpack and validate the data afterwards.
The unpack width enforces the field width you expect. If spaces can't occur between digits, it becomes even simpler. The matching regex would then be /^(\d+)$/. 'An' is the unpack template for a space-padded field of bytes and results in stripping the trailing spaces. In the regex, [\d ] is a character class of digits and spaces.# Given $record my %record; @record{ qw/account address info/ } = unpack 'A20 A42 A255', $record; # adjust widths to suit # ($record{'account'}) = $record{'account'} =~ /^(\d[\d ]*)$/ ($record{'account'}) = $record{'account'} =~ /^(\d+)$/ or die 'Bad Account ID'; # detaints, too # verify the rest
Update: Simplified the code to agree with leriksen's spec.
After Compline,
Zaxo
In reply to Re: Regex to match 20 chars of some digits followed by some spaces
by Zaxo
in thread Regex to match 20 chars of some digits followed by some spaces
by leriksen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |