0

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 :

  1. An*k - > Ankur and Ankit
  2. An*t -> Ankit
  3. An*k -> Ankur
  4. A * n *i -> Ankur
  5. 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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
user2332505
  • 619
  • 2
  • 10
  • 19

0 Answers0