• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 874
  • Last Modified:

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

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 !

C#
0
gsdevEE
Asked:
gsdevEE
  • 2
  • 2
1 Solution
 
elimesikaCommented:
Here it is

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

Open in new window

0
 
Patrick MatthewsCommented:
Hello gsdevEE,

Would the following be valid:

----
          
1234
abcd

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

Regards,

Patrick
0
 
Patrick MatthewsCommented:
elimesika,

I think you meant:

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

:)

Patrick
0
 
Fernando SotoRetiredCommented:
Hi  gsdevEE;

This regex pattern should do what you want.

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

Open in new window

0
 
Fernando SotoRetiredCommented:
I missed the part including white spaces.
^[a-zA-Z0-9 -]{1,12}$

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now