Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regex for a username with a few restrictions

Tags:

c#

regex

Similar to this topic.

I am trying to validate a username with the following restrictions:

  • Must start with a letter or number
  • Must be 3 to 15 characters in length
  • Symbols include: . - _ ( ) [ ]
  • Symbols cannot be adjacent, but letters and numbers can

Edit:

  • Letters and numbers are a-z A-Z 0-9

Been stumped for a while. I'm new to regex.

like image 340
Marlon Avatar asked Aug 27 '10 22:08

Marlon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is Lua C or C++?

Is Lua written in C++? The Lua interpreter is written in ANSI C.


1 Answers

As an optimization to Mark's answer:

^(?=.{3,15}$)([A-Za-z0-9][._()\[\]-]?)*$

Explanation:

(?=.{3,15}$)                   Must be 3-15 characters in the string
([A-Za-z0-9][._()\[\]-]?)*   The string is a sequence of alphanumerics,
                               each of which may be followed by a symbol

This one permits Unicode alphanumerics:

^(?=.{3,15}$)((\p{L}|\p{N})[._()\[\]-]?)*$

This one is the Unicode variant, plus uses non-capturing groups:

^(?=.{3,15}$)(?:(?:\p{L}|\p{N})[._()\[\]-]?)*$
like image 137
Ben Voigt Avatar answered Sep 22 '22 23:09

Ben Voigt