We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Valid Username Regular Expression
Valid Username Regular Expression
+ 0 comments I found the following regex to be working:-
"^[A-Za-z][a-zA-Z0-9_]{7,29}$" ;
+ 0 comments import java.io.; import java.util.; import java.util.regex.*; public class Solution {
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String empty = scanner.nextLine(); Pattern specialCharactersString = Pattern.compile("[!@#$,'^%&*()+=|<>?{}\\[\\]~-]"); Matcher hasSpecial; if (1 <= n && n <= 100) { String[] username = new String[n]; for (int i = 0; i < n; i++) { username[i] = scanner.nextLine(); } for (int i = 0; i < n; i++) { hasSpecial = specialCharactersString.matcher(username[i]); if ((username[i].length() >= 8) && (username[i].length() <= 30) && Character.isAlphabetic(username[i].charAt(0)) && !(hasSpecial.find())) { System.out.println("Valid"); } else { System.out.println("Invalid"); } } }
} }
+ 1 comment public static final String regularExpression = "[a-zA-Z]\\w{7,29}";
+ 0 comments you have to change only in "NULL" to this " ^[A-Za-z]\w{7,29}$"
+ 0 comments Here are the solution of Java Valid Username Regular Expression Hacker Rank Solution
Load more conversations
Sort 397 Discussions, By:
Please Login in order to post a comment