Skip to content
Advertisement

Regex for letters and numbers with any underscores in between

I’m trying to create a Regex String with the following rules

  1. The username is between 4 and 25 characters.
  2. It must start with a letter.
  3. It can only contain letters, numbers, and the underscore character.
  4. It cannot end with an underscore character.

when it meets this criterion I want the output to be true otherwise false, but I only get false for my test cases, here is my code

public class Profile {
    public static String username(String str) {


        String regularExpression = "^[a-zA-Z][a-zA-Z0-9_](?<=@)\w+\b(?!\_){4,25}$";

        if (str.matches(regularExpression)) {
            str = "true";
        }
        else if (!str.matches(regularExpression)) {
            str = "false";

        }
        return str;
    }

Main class

Profile profile = new profile();

Scanner s = new Scanner(System.in);
        System.out.print(profile.username(s.nextLine()));

input

"aa_"
"u__hello_world123"

output

false
false

Fixed: thanks to everyone who contributed

Advertisement

Answer

You can use

^[a-zA-Z][a-zA-Z0-9_]{3,24}$(?<!_)
^[a-zA-Z]w{3,24}$(?<!_)
^[a-zA-Z][a-zA-Z0-9_]{2,23}[a-zA-Z0-9]$
^p{Alpha}[a-zA-Z0-9_]{2,23}p{Alnum}$

See the regex demo.

Details:

  • ^ – start of string
  • [a-zA-Z] – an ASCII letter
  • [a-zA-Z0-9_]{3,24} / w{3,24} – three to twenty four ASCII letters, digits or underscores
  • $ – end of string
  • (?<!_) – a negative lookbehind that makes sure there is no _ (at the end of string).

Note that {3,24} is used and not {4,25} because the first [a-zA-Z] pattern already matches a single char.

Usage:

public static String username(String str) {
    return Boolean.toString( str.trim().matches("[a-zA-Z]\w{3,24}$(?<!_)") );
    // return Boolean.toString( str.trim().matches("\p{Alpha}[a-zA-Z0-9_]{2,23}\p{Alnum}") );
    // return Boolean.toString( str.trim().matches("[a-zA-Z][a-zA-Z0-9_]{2,23}[a-zA-Z0-9]") );
}

See Java demo:

import java.util.*;
import java.util.regex.*;

class Ideone
{
    public static String username(String str) {
        return Boolean.toString( str.trim().matches("[a-zA-Z]\w{3,24}$(?<!_)") );
    }
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(username("u__hello_world123")); // => true
        System.out.println(username("aa_")); // => false
    }
}
Advertisement