Creating usernames in a format (oneliner)

Hi everyone,

I want to build a single script that fetches a format that stands in a database. This format tells the script how to build an username. We have multiple customers, and every customer uses a different format for their usernames for example:

  • (first letter givenname).(last name without spaces) = f.lastnamewithoutspaces
  • (first name without spaces)(last name without spaces) = firstnamelastname

you get the idea. Does anyone know if you can build this into a single regular expression? I looked into it but it blew my mind and find it too difficult to wrap my head around since most of my finds were to validate if something was a username.

Hope you guys can help me out!

Kind regards,

I don’t think you’re going to be able to do this with a single regular expression. If I was doing this I’d get the format code and then use a switch statement with one case per format to then build the username. There are probably a limited number of formats you’d be using so make sure the codes are generic and can be used for multiple customers

Ok, do you mean that you would type the way it is supposed to be formatted in the database like:



and in your script use the validators to see which format is used so you can build upon the given variables?

Building usernames is usually much more complex. If there is a John.Smith and you pass another John Smith, you have to increment, add a middle initial, or something to make the username unique. Typically the process has to have a method to:

  • Generate the username
  • Validate the username is not in use
  • Increment username based on standards (add number, use middle initial, etc.)
  • Return the unique username to generate account

We typically have to build a function to return a unique username based on client requirements. It’s a process versus a rule\regex.

I’d just use a code to indicate the format to be used. The pseudo-code would be


switch (customerusercode){
code1 {create user name}
code2 {create user name}


create user