简体   繁体   English

Java中的通配符搜索

[英]Wild Card Search in Java

I am trying to implement wild cards in Java. 我正在尝试在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. 1)如果传递的字符串是“ a hello b”,而正则表达式是“ a * b”,则该函数应返回TRUE,因为在*处可以存在任意数量的字符。

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 ? 2)如果传递的字符串是“ a X b”并且正则表达式是“ a?b”,则返回值应为TRUE,因为如果存在? in regular expression then there should be only one character between a and b. 在正则表达式中,则a和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. 我不想导入Pattern和Matcher。 Without them I have to complete this. 没有他们,我必须完成这一工作。

Kindly, anyone help me on this by specifying the correct code. 好的,任何人都可以通过指定正确的代码来帮助我。

Thanking you 感谢您

Use regular expressions. 使用正则表达式。 Don't reinvent the wheel. 不要重新发明轮子。

As i said, you can do this extracting simple sub string's.Here I checked for one case, do same thing for other cases. 如我所说,您可以提取简单的子字符串。在这里我检查了一种情况,其他情况下也做同样的事情。

     public class Assign {

public boolean compare(String s1, String s2)
{ String s="";
    try
    {
 s=s1.substring(s1.indexOf('a')+2,s1.indexOf('b')-1);
    }
    catch(IndexOutOfBoundsException e)
    {
    return false;
    }
    System.out.println("string: "+s);

    if(s2.substring(2,3).equals("*")&&s.length()>=1)
    {return true;}
    else
        return false;

}
public static void main(String args[])
{
    Assign a = new Assign();
    boolean r = a.compare("a b hello", "a * b");
    System.out.println(r);
}
}

EDIT : May be i have not checked for all the cases.It's a way to approach. 编辑:可能是我还没有检查所有情况。这是一种处理方法。

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. 我编写了一个简单的程序,使用substring()indexOf() ,它可以根据要比较的字符串来评估regex

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 PS:有关许多此类问题,请参阅https://softwareengineering.stackexchange.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM