Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Username cannot contain repeating underscore or period

Tags:

regex

I have always struggled with these darn things. I recall a lecturer telling us all once that if you have a problem which requires you use regular expressions to solve it, you in fact now have 2 problems.

Well, I certainly agree with this. Regex is something we don't use very often but when we do its like reading some alien language (well for me anyway)... I think I will resolve to getting the book and reading further.

The challenge I have is this, I need to validate a username based on the following criteria:

  1. can contain letters, upper and lower
  2. can contain numbers
  3. can contain periods (.) and underscores (_)
  4. periods and underscores cannot be consecutive i.e. __ .. are not allowed but ._._ would be valid.
  5. a maximum of 20 characters in total

So far I have the following : ^[a-zA-Z_.]{0,20}$ but of course it allows repeat underscores and periods.

Now, I am probably doing this all wrong starting out with the set of valid characters and max length. I have been trying (unsuccessfully) to create some look-around or look-behind or whatever to search for invalid repetitions of period (.) and underscore (_) not sure what the approach or methodology to break down this requirement into a regex solution is.

Can anyone assist with a recommendation / alternative approach or point me in the right direction?

like image 686
SpaceKat Avatar asked Aug 03 '13 11:08

SpaceKat


People also ask

How do you underscore a username?

To make an underscore on most keyboards, hold the SHIFT key and press they hyphen/underscore key to the right of the zero (on the top number line).

What is username can only use letters numbers underscores and periods?

Usernames can contain letters (a-z), numbers (0-9), and periods (.). Usernames cannot contain an ampersand (&), equals sign (=), underscore (_), apostrophe ('), dash (-), plus sign (+), comma (,), brackets (<,>), or more than one period (.) in a row.

Can only contain letters numbers and underscores meaning in Hindi?

इसका मतलब है कि यूजरनेम लोअरकेस और अपरकेस में नाम का इस्तेमाल करता है । संख्या मजबूत सुरक्षा के लिए उपयोग किया जाता है और कोई भी हैक कर सकते हैं।


1 Answers

This one is the one you need:

^(?:[a-zA-Z0-9]|([._])(?!\1)){5,20}$

Regular expression visualization

Edit live on Debuggex

You can have a demo of what it matches here.


"Either an alphanum char ([a-zA-Z0-9]), or (|) a dot or an underscore ([._]), but that isn't followed by itself ((?!\1)), and that from 5 to 20 times ({5,20})."

  1. (?:X) simply is a non-capturing group, i.e. you can't refer to it afterwards using \1, $1 or ?1 syntaxes.

  2. (?!X) is called a negative lookahead, i.e. literally "which is not followed by X".

  3. \1 refers to the first capturing group. Since the first group (?:...){5,20} has been set as non-capturing (see #1), the first capturing group is ([._]).

  4. {X,Y} means from X to Y times, you may change it as you need.

like image 51
sp00m Avatar answered Oct 08 '22 13:10

sp00m