I want to write a regular expression that matches the following five format strings:
1. Number
2. Number-sharp
3. Number-sharp-sharp
4. Number-sharp*
5. Digits-sharp digits
question: how to write regular expressions that match the format strings in these 5? Please give me your advice.
I try to use [0-9] +-sharp | * | [0-9] and other matching methods are not allowed
public static boolean checkAccount(String email){
boolean flag = false;
try{
String check = "[0-9]+-sharp|*|[0-9]";
// String check = "\\d-sharp|*|[0-9]";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
flag = matcher.matches();
}catch(Exception e){
flag = false;
}
return flag;
}
// +-sharp|-sharp-sharp|-sharp*+
public static void main(String[] args) {
System.out.println(checkAccount("12334-sharp"));
System.out.println(checkAccount("12334-sharp-sharp"));
System.out.println(checkAccount("12334-sharp*"));
System.out.println(checkAccount("12334-sharp12367"));
System.out.println(checkAccount("123123"));
}