程序要求实现以下功能:
1。提示用户输入两个字符串,
2。读入用户输入的两个字符串
3。根据读入的两个字符串进行匹配,例如:
*i*tooer,dietooer 则说明两字符串匹配,返回true.
co*de,acode 则说明两字符串不匹配,返回false.请问这个功能用java语言如何实现?

解决方案 »

  1.   

    public static boolean isRegex(String s,String reg)//s为原字符串,reg为匹配字符串
    {
    return s.matches(reg.replaceAll("\\*", ".*"));
    }
      

  2.   

    import java.util.regex.*;public class TwoWords
    {
    public static void main(String[] args)
    {
    if(args.length!=2)System.out.println("wrong number of words");
    Pattern p=Pattern.compile(args[0]);
    Matcher m=p.matcher(args[1]);
    if(m.matches())System.out.println("匹配");
    }
    }
    运行:
    java TwoWords .*ab.*dd fdsfdsjab**dd
    打印:
    匹配
    我不知道lz说的*号是什么意思,是正则式中的星号还是普通的星号?
    还有就是运行 java TwoWords \\*ab.*dd *abw**dd不会打印匹配,这是什么原因啊?我觉得应该是匹配的啊