Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exact # of digits regular ex.

Status
Not open for further replies.

JNameNotTaken

Programmer
Aug 29, 2006
24
IE
Hi All

Sorry to have to ask this. but it's been driving me daft for the last few hours.

I need to verify that a scaler contains only digits and only 12 of them at that.

I've tried these (among others) to no avail:

if( $variable =~ m/\d\d\d\d\d\d\d\d\d\d\d\d/ )

if( $variable = /\d\d\d\d\d\d\d\d\d\d\d\d/ )

but those just won't work correctly, the number of digits entered can be any number and it will return true.

Any help will help keep me sane, thanks very much.


 
Code:
$variable='123456789012';
       
if( $variable =~ /^\d{12}$/ ){print 'got a twelver';}
else                         {print 'no dice';}

Works for me!
 
use string anchors like jrig does above:

^ - beginning of string
$ - end of string

otherwise all you are doing is checking if there are digits anywhere in the string.


- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top