How to detect exact length in regex?

Posted in Help the coder! on Apr 25, 2009 at 10:11 IST (about 1 year ago). Subscribe to this post Bookmark and Share Email
Showing comments 1 to 6 of total 6 on page 1 of 1
Tagged regex
Post reply
« Previous1Next »

manfred
Rank: 278

I have two regular expressions that validate the values entered.

One that allows any length of Alpha-Numeric value @"^\s*(?[A-Z0-9]+)\s*"

and the other only allows numerical values @"^\s*(?[0-9]{10})"

How can I get a numerical string of the length of 11 not to be catched by the NUM regex

Posted by manfred on Saturday, April 25, 2009, 10:11 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

danielle
Rank: 147

Match something non-numeric after the length 10 string. My regex-foo isn't that good, but I think you've got it setup there to catch a numeric string of exactly length 10, but since you don't match anything after that, a length 11 string would also match. Try matching beyond the end of the number and you'll be good.

Posted by danielle on Saturday, April 25, 2009, 11:35 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

micooh
Rank: 98

You could try alternation?

"^\s*(?\d{1,10}|\d{12,})"

Posted by micooh on Sunday, April 26, 2009, 11:39 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

limcaboy
Rank: 70

If it's single line, you could specify that your match must happen at the end of the line, like this in .net ...

^\s*([0-9]{10})\z

That will accept 1234567890 but reject 12345678901.

Posted by limcaboy on Tuesday, April 28, 2009, 12:38 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

gopalwins
Rank: 219

This should match only 10 digits and allow arbitrary numbers of whitespaces before and after the digits.

Non-capturing version: (only matches, the matched digits are not stored)

^\s*(?:\d{10})\s*$

Capturing version: (the matched digits are available in subgroup 1, as $1 or \1)

^\s*\d{10}\s*$
Posted by gopalwins on Wednesday, April 29, 2009, 2:46 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

samuel
Rank: 157

If you are trying to match only numbers that are 10 digits long, just add a trailing anchor using $, like this:

^\s*(?:[0-9]{10})\s*$

That will match any number that is exactly 10 digits long (with optional space on either side).

Posted by samuel on Wednesday, April 29, 2009, 10:01 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment
Pages: « Previous1Next »

Post your comment (No registration required)

  Add my comment  

TechieDesi Community

Not signed in (Sign-in or Register)
Be a true TechieDesi!
Top 10 Users
Spread the word
Invite your friends
Fan stuff
Help us improve
Need Help
FAQ's
Search tips
Found a bug? Report!
Feeds and letters
Subscribe via RSS
Archives
Subscribe to newsletter
Unsubscribe e-mail
Miscellaneous
Privacy policy
Visit rootnerve
About us
About us
Support the development
Official Blog
Advertise with us
Careers
Copyright (c) 2008, TechieDesi.com. All rights reserved | About us | Do-Not-Disturb registry | Powered by rootnerve | Page rendered in 0.195 seconds