Regex for 12 alphanumeric characters, a dash "-", and 1 to 12 characters in length, including whitespace

Posted on 2010-01-12
Medium Priority
Last Modified: 2013-12-17
Regex for 12 alphanumeric characters, a dash "-", and 1 to 12 characters in length, including whitespace.

So abc-12345678 would be valid

abc-123456789 would be invalid

And any string including any characters other than these would be invalid.

I cant figure the regex out.  Im sure there is someone who can get this quick, and I have too many bugs on my plate - thanks !

Question by:gsdevEE
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 19

Expert Comment

ID: 26294711
Here it is

[a-zA-Z -]{1,12}

Open in new window

LVL 93

Expert Comment

by:Patrick Matthews
ID: 26294894
Hello gsdevEE,

Would the following be valid:


They all meet the written criteria, but look somewhat different from your only example of a valid code...


LVL 93

Expert Comment

by:Patrick Matthews
ID: 26294925

I think you meant:

[a-zA-Z0-9 -]{1,12}


LVL 63

Expert Comment

by:Fernando Soto
ID: 26294952
Hi  gsdevEE;

This regex pattern should do what you want.


Open in new window

LVL 63

Accepted Solution

Fernando Soto earned 2000 total points
ID: 26294975
I missed the part including white spaces.
^[a-zA-Z0-9 -]{1,12}$

Open in new window


Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question