biryu2205 / Biryu

0 stars 0 forks source link

Hackerrank Valid Username Regular Expression #85

Closed biryu2205 closed 6 years ago

biryu2205 commented 7 years ago
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Solution {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int testCases = Integer.parseInt(in.nextLine());
    while (testCases > 0) {
      String username = in.nextLine();

      String pattern = "^[a-zA-Z][a-zA-Z0-9_]{7,29}$";

      Pattern r = Pattern.compile(pattern);
      Matcher m = r.matcher(username);
      if (m.find()) {
        System.out.println("Valid");
      } else {
        System.out.println("Invalid");
      }
      testCases--;
    }
  }
}

Link - https://www.hackerrank.com/challenges/valid-username-checker/problem