Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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_File - Fatal编程技术网

正则表达式和单词列表Java

正则表达式和单词列表Java,java,regex,file,Java,Regex,File,我必须向用户询问要检索的单词的特定模式 例如,如果用户输入 #5:表示大小为5的英语单词 #4=at:表示长度为4的英语单词,包含子字符串at。包括聊天,, 速率 #6-^^y:指长度为6的英语单词,以两个元音的子串结尾 后面跟着字母“y” #5+*ro:表示长度为5的英语单词,其开头的子字符串具有非- 元音字母后跟子字符串“ro”。这包括破碎、冻结、书写 我正确处理了文件部分,但无法处理正则表达式部分 这是我的密码 import java.io.File; import java.io.Fil

我必须向用户询问要检索的单词的特定模式 例如,如果用户输入

#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();
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.Scanner;
公共类替换应用程序{
公共静态void main(字符串参数[])
{
ReplaceApp rf=新的ReplaceApp();
扫描仪输入=新扫描仪(系统输入);
字符串模式;
rf.openFile();
rf.readData();
System.out.println(“输入要检索单词的模式”);
System.out.println(“如果需要帮助,请键入\“?\”);
pattern=in.nextLine();
if(模式等于(“?”)
{
System.out.println(“-符号*只能替换为非元音字母”);
System.out.println(“-符号^只能由元音字母替换”);
System.out.println(“-符号&只能由元音字母或无元音字母替换”);
System.out.println(“-一种特殊模式,以#开头,后跟一个整数,后面可以跟一个正数,”
+“负号或等号后跟前面解释过的图案,表示长度相同的英语单词”
+“在#之后指定,并包含所述模式作为其子字符串。子字符串位于”
+如果符号为正,则在单词的开头;如果符号为负,则在单词的结尾;以及
+“符号等于的任何地方。”);
}
if(pattern.startsWith(“*”))
{
System.out.println(rf.retrieveWords(^[b|c|d|f|g|h|j|k|l|m|n|p|s|t|v|w|x|y|z]);
}
if(pattern.startsWith(“^”))
{
System.out.println(rf.retrieveWords(“^[aeuio]”);
}
}
扫描仪输入;
ArrayList wordList=新建ArrayList();;
公共void openFile(){
试一试{
输入=新扫描仪(新文件(“words.txt”);
}//结束尝试
捕获(FileNotFoundException FileNotFoundException){
System.out.println(“打开文件时出错”);
}//端盖
}//结束方法openFile
public void readData(){
//使用Scanner对象从文件中读取记录
while(input.hasNext()){
add(input.nextLine());
}//结束时
input.close();
}//结束方法readRecords
公共对象[]检索词(字符串re)
{
ArrayList wordsToFind=新的ArrayList();
对于(字符串字:单词列表){
if(单词匹配(re))
wordsToFind.add(word);
} 
返回单词stofind.toArray();
}
}

以下是一些正则表达式模式

#5: means an English word of size 5
\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


模式解释

\b             A word boundary

\w             A word character: [a-zA-Z_0-9]

X{n}           X, exactly n times

[abc]          a, b, or c (simple class)

[^abc]         Any character except a, b, or c (negation)

研究每种模式的解释。

用户输入的模式如何听起来像是在定义自己的模式语言。您这样做而不使用标准正则表达式语法有什么特殊原因吗?@user3590565您必须将用户模式转换为标准正则表达式模式。请访问以创建/测试您的正则表达式。@merlin2011我想。但是我不知道怎么做。你的第一个字母必须是
\b\w{5}\b
才能只匹配5个字母单词。但是
\b
忽略标点符号。(OP对他的词源非常模糊——它可能是一个单词列表,也可能是现实世界的文本。)但我想问的是,如果用户输入了他的模式,你所说的用户模式是什么意思?用户知道java正则表达式模式吗?为什么不为用户制定一些规则,并在后台将其转换为java正则表达式呢
#6-^^y: means an English word of length six and it ends with 
the substring of two vowels followed by the letter ‘y’
#5+*ro: means an English word of length five and it starts with 
the substring having a non- vowel letter followed by the substring ‘ro’. 
This includes broke, froze, wrote, ..
\b             A word boundary

\w             A word character: [a-zA-Z_0-9]

X{n}           X, exactly n times

[abc]          a, b, or c (simple class)

[^abc]         Any character except a, b, or c (negation)