Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Regex to match 20 chars of some digits followed by some spaces

by Zaxo (Archbishop)
on Dec 19, 2003 at 03:32 UTC ( [id://315735]=note: print w/replies, xml ) Need Help??


in reply to Regex to match 20 chars of some digits followed by some spaces

You can take advanntage of the fixed width property with unpack and validate the data afterwards.

# 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
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.

Update: Simplified the code to agree with leriksen's spec.

After Compline,
Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://315735]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (None)
    As of 2025-01-18 11:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Which URL do you most often use to access this site?












      Results (56 votes). Check out past polls.