Q:

Wild Card Search in Java

I am trying to implement wild cards in Java.
Here is the code I have :
public class Assign {

public boolean compare(String s1, String s2)
{
    char [] s3 = s1.toCharArray();
    char [] s4 = s2.toCharArray();
    int i,j;

    int k = 0;
    for(i=0;i<s3.length;i++)
    {
        for(j=0;j<s4.length;j++)
        {
            if(s3[i] == s4[j])
            {

                if(s4[j] == '*')
                {
                    i++;

                    if(s3[i] == s4[s4.length-1])
                    {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
public static void main(String args[])
{
    Assign a = new Assign();
    boolean r = a.compare("a hello b", "a * b");
    System.out.println(r);
}
}

There will be two parameters to be passed to the function. One is a string and the other one is a regular expression.
Example cases are:
1) If string passed is "a hello b" and the regular expression is "a * b" then the function should return TRUE because in the place of * any number of characters can exist.
2) If string passed is "a X b" and the regular expression is "a ? b" then the return value should be TRUE because if there is a ? in regular expression then there should be only one character between a and b.
Like this it shall work for all cases. I think the logic I thought is fine but I ma having a trouble in the coding part.
I dont want to import Pattern and Matcher. Without them I have to complete this.
Kindly, anyone help me on this by specifying the correct code.
Thanking you

A:

Use regular expressions. Don't reinvent the wheel.

A:

Assuming that the regex will only contain either * or ? at any
  instance of time

I have written a simple program using substring() & indexOf(), which tires to evaluate the regex against the string to compare.
package problems;

public class WildCardCompare {

    public boolean compare(String str, String regex) {
        if(regex == null || str == null) {
            return false;
        }
        if(regex.equals("*")) {
            return true;
        }
        if(regex.equals("?") && str.length() == 1) {
            return true;
        }
        if(!regex.contains("*") && !regex.contains("?")) {
            return str.equals(regex);
        }

        String token = null;
        if(regex.contains("*")) {
            token = "*";
        }

        if(regex.contains("?")) {
            token = "?";
        }

        if(token != null) {
            //String before *, if any...
            String before = regex.substring(0, regex.indexOf(token));
            //String after *, if any...
            String after = regex.substring(regex.indexOf(token)+1, regex.length());

            boolean bmatches = true;
            if(before != null && before.length() != 0) {
                if(str.indexOf(before) == 0) {
                    bmatches = true;
                }
                else {
                    bmatches = false;
                }
            }
            boolean amatches = true;
            if(after != null && after.length() != 0) {
                if(str.indexOf(after) == (str.length() - after.length())) {
                    amatches = true;
                }
                else {
                    amatches = false;
                }
            }
            return bmatches && amatches;
        }

        return false;
    }

    public static void main(String args[])
    {
        boolean r;
        WildCardCompare compare = new WildCardCompare();
        r = compare.compare("a b hello", "a b *");
        System.out.println(r);
        r = compare.compare("a hello b", "a * b");
        System.out.println(r);
        r = compare.compare("a hello b", "aaaa*bbbb");
        System.out.println(r);
        r = compare.compare( "aaaaTbbbb", "aaaa*bbbb");
        System.out.println(r);
        r = compare.compare( "aT", "a?");
        System.out.println(r);
        r = compare.compare("AT",  "a?");
        System.out.println(r);
        r = compare.compare( "aXb", "a?b");
        System.out.println(r);
        r = compare.compare( "abc", "xyz");
        System.out.println(r);
    }
}

And here's the output.
true
true
false
true
true
false
true
false

I feel this program is NOT 'fool proof' and can only be taken as an example to solve the regex matching problem
PS: Please see https://softwareengineering.stackexchange.com/ for many such kinds of problems

