如何使用正则表达式查找格式?我可以找到一个特定的单词,但我想搜索这样的东西: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]+");}
答案 0 :(得分:0)
以a和第三个字母开头的字符串的正则表达式
^a.t
正则表达式为4个字母单词,以w
开头,以rd
^w[a-z]rd$