Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: adding characters via a regular expression

by haoess (Curate)
on Apr 21, 2004 at 17:00 UTC ( #347063=note: print w/ replies, xml ) Need Help??


in reply to adding characters via a regular expression

Assumung users are entering letters, whitespace and other stuff, and you want only the first numbers (maximal 9), use

$number =~ s/.*?(\d{1,9}).*/sprintf "%09d", "$1"/e;

This does not work if users enters no digit. The best way to handle this is to perform not just a single check:

my $number = <STDIN>; if( $number !~ /\d/ ) { print "no number"; } else { # clean up and pad input }

-- Frank


Comment on Re: adding characters via a regular expression
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2015-07-02 22:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (46 votes), past polls