diff --git a/email_registration.install b/email_registration.install
index 1190491..773c23e 100644
--- a/email_registration.install
+++ b/email_registration.install
@@ -29,4 +29,5 @@ function email_registration_requirements() {
  */
 function email_registration_uninstall() {
   variable_del('email_registration_login_with_username');
+  variable_del('email_registration_username_pattern');
 }
diff --git a/email_registration.module b/email_registration.module
index d2e9641..6a33e59 100644
--- a/email_registration.module
+++ b/email_registration.module
@@ -22,10 +22,21 @@ function email_registration_user_insert(&$edit, &$account, $category = NULL) {
   $names = array_filter($names);
 
   if (empty($names)) {
-    // Strip off everything after the @ sign.
-    $new_name = preg_replace('/@.*$/', '', $edit['mail']);
-    // Clean up the username.
-    $new_name = email_registration_cleanup_username($new_name, $account->uid);
+    $username_pattern = variable_get('email_registration_username_pattern', '');
+
+    if (!empty($username_pattern)) {
+      // If username pattern setting exists then set name as per the pattern.
+      $new_name = token_replace($username_pattern, array('user' => $account));
+
+      // Clean up the username.
+      $new_name = email_registration_cleanup_username($new_name, $account->uid);
+    }
+    else {
+      // Strip off everything after the @ sign.
+      $new_name = preg_replace('/@.*$/', '', $edit['mail']);
+      // Clean up the username.
+      $new_name = email_registration_cleanup_username($new_name, $account->uid);
+    }
   }
   else {
     // One would expect a single implementation of the hook, but if there
@@ -164,7 +175,61 @@ function email_registration_form_user_admin_settings_alter(&$form, &$form_state)
     '#title' => t('Allow users login with e-mail or username.'),
     '#description' => t('Allow users to login with their username in addition to their e-mail.'),
     '#default_value' => variable_get('email_registration_login_with_username', TRUE),
+  );
+
+  $form['registration_cancellation']['configure_username'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Configure username pattern'),
+    '#default_value' => variable_get('email_registration_username_pattern', ''),
+    '#description' => t('Pattern that will be used to create username'),
+  );
+
+  if (module_exists('token')) {
+    $form['registration_cancellation']['token_help'] = array(
+      '#theme' => 'token_tree',
+      '#token_types' => array('user'),
+      '#show_restricted' => TRUE,
+      '#dialog' => TRUE,
     );
+  }
+  else {
+    // Manually show available tokens.
+    $header = array(
+      array('data' => t('Token')),
+      array('data' => t('Description')),
+    );
+
+    $rows = array();
+    $rows[] = array(
+      array('data' => '[user:mail]'),
+      array('data' => t('The email address of the user account.')),
+    );
+    $rows[] = array(
+      array('data' => '[user:name]'),
+      array('data' => t('The login name of the user account.')),
+    );
+    $rows[] = array(
+      array('data' => '[user:uid]'),
+      array('data' => t('The unique ID of the user account.')),
+    );
+    $rows[] = array(
+      array('data' => '[site:name]'),
+      array('data' => t('The name of the site.')),
+    );
+
+    $form['registration_cancellation']['token_help'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Available tokens'),
+      '#collapsible' => TRUE,
+      '#collapsed' => TRUE,
+    );
+    $form['registration_cancellation']['token_help']['help'] = array(
+      '#theme' => 'table',
+      '#header' => $header,
+      '#rows' => $rows,
+    );
+  }
+
   $form['#submit'][] = 'email_registration_form_user_admin_settings_submit';
 }
 
@@ -175,6 +240,7 @@ function email_registration_form_user_admin_settings_alter(&$form, &$form_state)
  */
 function email_registration_form_user_admin_settings_submit($form, &$form_state) {
   variable_set('email_registration_login_with_username', $form_state['values']['email_registration_login_with_username']);
+  variable_set('email_registration_username_pattern', $form_state['values']['configure_username']);
 }
 
 
