Java中的正则表达式,匹配存储在ArrayList中

时间:2014-05-17 16:04:43

标签: java regex arraylist

我制作了以下代码,目的是存储和显示以字母a开头并以z结尾的所有字词。首先,我从我的正则表达式模式中得到一个错误,其次我收到一个错误,因为没有显示存储在ArrayList中的内容(String)。

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.*;


public class RegexSimple2{

    public static void main(String[] args) {
        try{
            Scanner myfis = new Scanner("D:\\myfis2.txt");
            ArrayList <String> foundaz = new ArrayList<String>();
            while(myfis.hasNext()){
                String line = myfis.nextLine();
                String delim = " ";
                String [] words = line.split(delim);
                 for ( String s: words){
                    if(!s.isEmpty()&& s!=null){
                        Pattern pi = Pattern.compile("[a|A][a-z]*[z]");
                        Matcher ma = pi.matcher(s);
                        boolean search = false;
                        while (ma.find()){
                            search = true;
                            foundaz.add(s);
                        }
                        if(!search){
                            System.out.println("Words that start with a and end with z have not been found");
                        }
                    }
                }
            }

            if(!foundaz.isEmpty()){
                for(String s: foundaz){
                    System.out.println("The word that start with a and ends with z is:" + s + " ");
                }
            }
        }
        catch(Exception ex){
            System.out.println(ex);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要更改阅读文件的方式。此外,将正则表达式更改为[aA].*z.*匹配零个或多个任何内容。请参阅我在下面做的小改动:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.*;

public class Test {

    public static void main(String[] args) {

        try {
            BufferedReader myfis = new BufferedReader(new FileReader("D:\\myfis2.txt"));
            ArrayList<String> foundaz = new ArrayList<String>();

            String line;
            while ((line = myfis.readLine()) != null) {
                String delim = " ";
                String[] words = line.split(delim);

                for (String s : words) {                    
                    if (!s.isEmpty() && s != null) {
                        Pattern pi = Pattern.compile("[aA].*z");
                        Matcher ma = pi.matcher(s);

                        if (ma.find()) {
                           foundaz.add(s);
                        }
                    }
                }
            }

            if (!foundaz.isEmpty()) {
                System.out.println("The words that start with a and ends with z are:");
                for (String s : foundaz) {
                    System.out.println(s);
                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

输入是:

apple
applez
Applez
banana

输出是:

The words that start with a and ends with z are:
applez
Applez

答案 1 :(得分:0)

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.*;

public  class RegexSimple2
{

     public static void main(String[] args) {   

          try
          {
         Scanner myfis = new Scanner(new File("D:\\myfis2.txt"));
          ArrayList <String> foundaz = new ArrayList<String>();


        while(myfis.hasNext())
        {
            String line = myfis.nextLine();
            String delim = " ";
            String [] words = line.split(delim);



     for (String s : words) {                    
                    if (!s.isEmpty() && s != null) 
                    {
                        Pattern pi = Pattern.compile("[aA].*z");
                        Matcher ma = pi.matcher(s);

                        if (ma.find()) {
                           foundaz.add(s);
                        }
                    }
                }
            }
                    if(foundaz.isEmpty())
                {
                    System.out.println("No matching words have been found!");
                }
                    if(!foundaz.isEmpty())
                    {
                        System.out.print("The words that start with a and ends with z are:\n");
                        for(String s: foundaz)
                        {
                            System.out.println(s);
                }

        }

    }





catch(Exception ex)
     {
        System.out.println(ex); 
     }
}
}