title: Java Wildcard Separator
body: <p>I have recently been working on Wildcard Pattern Matching where the format is:</p>
  
  <pre><code>"http(yyy|xxx|zzz|fssadasd|asdasd|asdasdas|asdasdasd|://)www.w3schools(.com|.org|.net).*"
  </code></pre>
  
  <p>for the data:</p>
  
  <p><a href="http://www.w3schools.com/html/tryit.asp?filename=tryhtml_default" rel="nofollow">http://www.w3schools.com/html/tryit.asp?filename=tryhtml_default</a></p>
  
  <p>I have developed a pattern matching function to execute when the format is single bracketed(multiple or operators). How do i get to store all possible combinations of the above mentioned format(multiple brackets and multiple operators).</p>
  
  <p>Some of the Possible combinations:</p>
  
  <p>httpyyyw3schools.com.*</p>
  
  <p>httpxxxw3schools.com.*</p>
  
  <p>httpzzzw3schools.com.*</p>
  
  <p>httpyyyw3schools.net.*</p>
  
  <p>httpxxxw3schools.net.*</p>
  
  <p>httpzzzw3schools.net.* etc.,</p>
  
  <p>Please do let me know if the question is still unclear?</p>
  
  <p>The Pattern Matching Function is:</p>
  
  <pre><code>import java.util.*;
  import java.util.regex.Pattern;
  import java.util.regex.Matcher;
  public class wildcardtest
  {
      public static void main(String[] args) 
      {
  
      test("h*p(://|xxx)ww*", "http://www.w3schools.com/html/tryit.asp?filename=tryhtml_default"); 
      test("h*p(yyy|://)ww*", "http://www.w3schools.com/html/tryit.asp?filename=tryhtml_default"); 
      test("h*p(yyy|xxx|zzz|fssadasd|asdasd|asdasdas|asdasdasd|://)ww*", "http://www.w3schools.com/html/tryit.asp?filename=tryhtml_default"); 
      test("ge?ks*", "geeksforgeeks");
      test("g*k", "gee");
      test("*pqrs", "pqrst");
      test("abc*bcd", "abcdhghgbcd");
      test("abc*c?d", "abcd");
      test("*c*d", "abcd");
      test("*?c*d", "abcd");
      test("*", "abcd");
      test("?", "a");
      }
  
      static boolean matches(String format, String data) {
  
          if(format.contains("|"))
          {
  
              int openpos = format.indexOf("(");
              int closepos = format.indexOf(")");
  
              String match = "";
              Pattern p = Pattern.compile("\\((.*?)\\)");
              Matcher m = p.matcher(format);
  
                  while(m.find()) 
                  {
                      match = m.group(1);
                  }
  
              String after = format.substring(closepos+1);
              String[] spliiter = match.split(Pattern.quote("|"));
  
              for(int i = 0;i&lt;spliiter.length;i++)
              {
                   StringBuilder before = new StringBuilder(format.substring(0,openpos));
                   before.append(spliiter[i]).append(after);      
  
                   if (matches(before.toString(),data)) {
                      return true;
                   }
  
              }           
              return false;
          }
  
  
  
      if (format.length() == 0 &amp;&amp; data.length() == 0)
          return true;
  
      if (format.length() == 1 &amp;&amp; format.charAt(0) == '*')
          return true;
  
      if (format.length() == 0 &amp;&amp; data.length() &gt; 0)
          return false;
  
      if (format.charAt(0) == '*' &amp;&amp; format.charAt(1) != 0 &amp;&amp; data.length() == 0)
          return false;
  
      if (format.charAt(0) == '?' || format.charAt(0) == data.charAt(0))
          return matches(format.substring(1), data.substring(1));
  
      if (format.charAt(0) == '*')
          return matches(format.substring(1), data) || matches(format, data.substring(1));
  
      return false;
  }
  
      static void test(String first, String second)
      { 
          System.out.println(matches(first, second)); 
      }
  
  }
  </code></pre>