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

时间:2014-05-14 09:58:18

标签: java regex

如何使用正则表达式查找格式?我可以找到一个特定的单词,但我想搜索这样的东西:a?t *(这应该是艺术的匹配,注意 - ?表示一个字母和* n个字母,但只能放在单词的结尾处)。

那我该怎么做呢?

我不知道怎么把这个让你理解。我想使用像w?rd这样的格式搜索一个单词(这假设匹配单词或者单位)。

我正在寻找的是这样的:

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]+");}

1 个答案:

答案 0 :(得分:0)

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

^a.t

正则表达式为4个字母单词,以w开头,以rd

结尾
^w[a-z]rd$