正则表达式和单词列表Java
我必须向用户询问要检索的单词的特定模式 例如,如果用户输入
#5:表示大小为5的英语单词
#4=at:表示长度为4的英语单词,包含子字符串at。包括聊天,, 速率
#6-^^y:指长度为6的英语单词,以两个元音的子串结尾 后面跟着字母“y”
#5+*ro:表示长度为5的英语单词,它以具有非- 元音字母后跟子字符串“ro”。这包括破碎、冻结、书写
我正确处理了文件部分,但无法处理正则表达式部分
这是我的密码
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ReplaceApp {
public static void main (String args[])
{
ReplaceApp rf = new ReplaceApp();
Scanner in = new Scanner(System.in);
String pattern;
rf.openFile();
rf.readData();
System.out.println("Enter the pattern that you wish to retrieve words of");
System.out.println("If you want help type \"?\"");
pattern=in.nextLine();
if (pattern.equals("?"))
{
System.out.println("- The symbol * can only be replaced by a none vowel letter");
System.out.println("- The symbol ^ can only be replaced by a vowel letter");
System.out.println("- The symbol & can only be replaced by a vowel or none vowel letter");
System.out.println("- A special pattern that starts with # followed by an integer and can be followed by a positive, "
+ "negative or equal sign followed by a pattern as explained earlier means an English word of the length "
+ "specified after # and contains the described pattern as substring of it. The substring is at the "
+ "beginning of the word if the sign is positive, at the end of the word if the sign is negative, and "
+ "anywhere if the sign is equals.");
}
if (pattern.startsWith("*"))
{
System.out.println(rf.retrieveWords("^[b|c|d|f|g|h|j|k|l|m|n|p|q|r|s|t|v|w|x|y|z]"));
}
if (pattern.startsWith("^"))
{
System.out.println(rf.retrieveWords("^[aeuio]"));
}
}
Scanner input;
ArrayList<String> wordList=new ArrayList<String>();;
public void openFile() {
try {
input = new Scanner(new File("words.txt"));
} // end try
catch (FileNotFoundException fileNotFoundException) {
System.out.println("Error opening file.");
} // end catch
} // end method openFile
public void readData() {
// read records from file using Scanner object
while (input.hasNext()) {
wordList.add(input.nextLine());
} // end while
input.close();
} // end method readRecords
public Object[] retrieveWords(String re)
{
ArrayList<String> wordsToFind=new ArrayList<String>();
for(String word:wordList){
if(word.matches(re))
wordsToFind.add(word);
}
return wordsToFind.toArray();
}
}
# 1 楼答案
下面是一些正则表达式模式
\b\w{5}\b
\bat\w{2}\b|\b\wat\w\b|\b\w{2}at\b
\b\w{3}[aeiou]{2}y\b
\b[^aeiou]ro\w{2}\b
模式解释
研究Java Regex Pattern对每种模式的解释