I am trying to implement a API which would search a user name, supporting wildcard characters.
For Example : If I have 2 user : Ankur and Ankit, then following regex support is valid :
- An*k - > Ankur and Ankit
- An*t -> Ankit
- An*k -> Ankur
- A * n *i -> Ankur
- A * n *t -> Ankit
I tried the following regex :
public static void main(String[] args)
{
String input = "A*n*i";
Pattern pattern = Pattern.compile("(^\\*?)(\\w+)(\\*?)(\\w+)(\\*?)");
Matcher matcher = pattern.matcher(input);
while (matcher.find())
{
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
System.out.println(matcher.group(5));
// Does not capture character i.
}
}
The regex work , when there is only one * in between. Now if number of asterisk increases in between,then I am not able to get the characters in between these asterisks.
Asterisk between characters can repeat multiple time, hence I require your help in designing a regex , that can capture characters between all of these asterisks.