How can I test a string is a valid ussd code? I was trying to write my own regex but failed.
It should start with *
symbol followed by any integers or another one *
. Star symbol can not repeat. It mustn't have **
. And the last symbol must be #
. Also this symbol is allowed once.
My tests:
public static final Pattern USSD = Pattern.compile("Pattern here");
System.out.println(USSD.matcher("*123#").matches()); // Must be true
System.out.println(USSD.matcher("*1*2*3#").matches()); // Must be true
System.out.println(USSD.matcher("*1#23#").matches()); // Must be false
System.out.println(USSD.matcher("***123###").matches()); // Must be false
System.out.println(USSD.matcher("*12**3#").matches()); // Must be false
What I've already tried:
- This answer is not what I'm looking for. It allows dual
*
and dual#
^\*([0-9]|\*?)*#$
doesn't help with dual stars