Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在字典里搜索。使用正则表达式_Java_Regex - Fatal编程技术网

Java 在字典里搜索。使用正则表达式

Java 在字典里搜索。使用正则表达式,java,regex,Java,Regex,如何使用正则表达式查找格式?我可以找到一个特定的单词,但我想搜索如下内容:a?t*(这应该与art匹配,attation-?表示一个字母和*n个字母,但只能放在单词的末尾) 那我该怎么做呢 我不知道该怎么说才能让你明白。我想用w?rd(假设与word或ward匹配)这样的格式搜索一个单词 我要找的是这样的东西: public boolean regexChecker(String theRegex, String str2Check){ // You define your regul

如何使用正则表达式查找格式?我可以找到一个特定的单词,但我想搜索如下内容:a?t*(这应该与art匹配,attation-?表示一个字母和*n个字母,但只能放在单词的末尾)

那我该怎么做呢

我不知道该怎么说才能让你明白。我想用w?rd(假设与word或ward匹配)这样的格式搜索一个单词

我要找的是这样的东西:

public boolean regexChecker(String theRegex, String str2Check){

    // You define your regular expression (REGEX) using Pattern

    Pattern checkRegex = Pattern.compile(theRegex);

    // Creates a Matcher object that searches the String for
    // anything that matches the REGEX

    Matcher regexMatcher = checkRegex.matcher( str2Check );

    // Cycle through the positive matches and print them to screen
    // Make sure string isn't empty and trim off any whitespace

    while ( regexMatcher.find() ){
        if (regexMatcher.group().length() == str2Check.length()){
            System.out.println( regexMatcher.group().trim() );

            // You can get the starting and ending indexs

            System.out.println( "Start Index: " + regexMatcher.start());
            System.out.println( "Start Index: " + regexMatcher.end());
            return true;
        }
    }

    System.out.println();
    return false;
}
public String regexReplace(String str2Replace){



    Pattern replace = Pattern.compile("\\?");

    Matcher regexMatcher = replace.matcher(str2Replace.trim());

    return regexMatcher.replaceAll("[A-Za-z]");

}
public String regexReplace2(String str2Replace){


Pattern replace = Pattern.compile("\\*");



Matcher regexMatcher = replace.matcher(str2Replace.trim());


return regexMatcher.replaceAll("[A-Za-z]+");}

以a和第三个字母t开头的字符串的正则表达式

^a.t
w
开头,以
rd
结尾的4个字母单词的正则表达式

^w[a-z]rd$

如上所述,您的问题不够清楚,无法理解。
a?t*
既不匹配“art”也不匹配“attention”,因此我建议您从更仔细地阅读正则表达式语法指南开始。“?”表示出现0或1个“previous”字符。您需要“a.*t.*”和更多的正则表达式知识。@Laurentiu-是的。如果您知道如何正确使用正则表达式,则可以使用正则表达式。句号。所以你想要某种通配符?它通常使您更容易理解您想要的内容,并提供清晰的输入和输出示例。另外,
如果您不明白问题
不是StackOverflow的工作原理,请不要再发表评论。通过编辑评论来澄清你的问题,你会得到答案:清楚解释的责任在你身上。