java - 使用模式匹配从文件中排序,Java

标签 java regex sorting pattern-matching filereader

所以我已经让我的程序正确地分隔了文本文件的行,甚至可以匹配第一行文本的模式,但我还需要能够检测和分隔地址行文本文件并根据其方向或街道/百老汇对它们进行排序,但我什至无法获取要检测的地址设置的初始模式。我使用正则表达式是否错误,这就是地址部分无法被正确检测到的原因吗?

代码

package csi311;

// Import some standard Java libraries.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
/**
 * Hello world example.  Shows passing in command line arguments, in this case a filename. 
 * If the filename is given, read in the file and echo it to stdout.
 */
public class HelloCsi311 {

    /**
     * Class construtor.
     */
    public HelloCsi311() {
    }


    /**
     * @param filename the name of a file to read in 
     * @throws Exception on anything bad happening 
     */
    public void run(String filename) throws Exception {
        if (filename != null) {
            readFile(filename); 
        }
    }


    /**
     * @param filename the name of a file to read in 
     * @throws Exception on anything bad happening 
     */
    private void readFile(String filename) throws Exception {
        System.out.println("Dumping file " + filename); 
        // Open the file and connect it to a buffered reader.
        BufferedReader br = new BufferedReader(new FileReader(filename));  
        ArrayList<String> foundaddr = new ArrayList<String>();
        String line = null;  
        String pattern = "^\\d\\d\\d-[A-Za-z][A-Za-z][A-Za-z]-\\d\\d\\d\\d";
        String address[] = new String[4];
        address[0] = "\\d{1,3}\\s\\[A-Za-z]{1,20}";
        address[1] = "\\d{1,3}\\s\\[A-Za-z]{1,20}\\s\\d{1,3}\\[A-Za-z]{1,20}\\s\\[A-Za-z]{1,20}";
        address[2] = "\\d{1,3}\\s\\d{1,3}\\[A-Za-z]{1,20}\\s\\[A-Za-z]{1,20}";
        address[3] = "\\d\\d\\s\\[A-Za-z]{1,20}";
        Pattern r = Pattern.compile(pattern);
        // Get lines from the file one at a time until there are no more.
        while ((line = br.readLine()) != null) {
            if(line.trim().isEmpty()) {
                continue;
            }
            String sample = line.replaceAll("\\s+,", ",").replaceAll(",+\\s",",");
            String[] result = sample.split(",");
            String pkgId = result[0].trim().toUpperCase();
            String pkgAddr = result[1].trim();


            Float f = Float.valueOf(result[2]);
            for(String str : result){
                // Trying to match for different types
                for(String pat : address){
                    if(str.matches(pat)){
                        System.out.println(pat);
                    }
                }



                if(f < 50 && !pkgId.matches(pattern)) {
                    Matcher m = r.matcher(str);
                    if(m.find()) {
                        foundaddr.add(str);
                    }
                }
            }
        }

        if(foundaddr != null) {
            System.out.println(foundaddr.size());
        }   

        // Close the buffer and the underlying file.
        br.close();
    }



    /**
     * @param args filename
     */
    public static void main(String[] args) {
        // Make an instance of the class.
        HelloCsi311 theApp = new HelloCsi311();
        String filename = null; 
        // If a command line argument was given, use it as the filename.
        if (args.length > 0) {
            filename = args[0]; 
        }
        try { 
            // Run the run(), passing in the filename, null if not specified.
            theApp.run(filename);
        }
        catch (Exception e) {
            // If anything bad happens, report it.
            System.out.println("Something bad happened!");
            e.printStackTrace();
        }    
    }
}

文本文件

123-ABC-4567, 15 W. 15th St., 50.1
456-BGT-9876,22 Broadway,24
QAZ-456-QWER, 100 East 20th Street,50
Q2Z-457-QWER, 200 East 20th Street, 49
6785-FGH-9845 ,45 5th Ave, 12.2,
678-FGH-9846 ,45 5th Ave, 12.2

123-ABC-9999, 46 Foo Bar, 220.0
347-poy-3465, 101 B'way,24

下面的代码行应该能够处理地址行,但由于某种原因,它与正确分隔地址行的模式和输出不匹配,可以在处理循环上方的 print 语句中看到地址,但由于某种原因,地址行甚至没有被检测为匹配,我很困惑为什么会这样。

代码行问题在于

  for(String str : result){
      //System.out.println(str);
      // Trying to match for different types
      for(String pat : address){
          if(str.matches(pat)){
              System.out.println(pat);
          }
      }

所需输出 - 按要求编辑 -

22 Broadway
45 5th Ave
101 B'way

最佳答案

我相信问题出在你的正则表达式上。 \\d\\d\\s\\[A-Za-z]{1,20} 例如,所有转义后都变成 \d\d\s\[ A-Za-z]{1,20}。分割如下:

  • \d:匹配任意数字
  • \d:匹配任意数字
  • \s:匹配任意空白字符
  • \[:匹配[字符
  • A-Za-z:匹配文字文本A-Za-z
  • ]:匹配文字字符]
    • {1,20}:匹配前面的字符 (]) 1-20 次。

您可能想要的正则表达式是 \d\d\s[A-Za-z]{1,20} ,作为转义字符串是 \\d\\d\\s[A-Za-z]{1,20}。请注意,[ 之前没有 \

还需要记住的是正则表达式可以匹配字符串中的任何位置。例如,正则表达式 a 将匹配字符串 a,但也会匹配 abcbacabracadabra 等。为了避免这种情况,必须使用锚定符号 ^$ 分别匹配开始和结束。然后,您的正则表达式将变为 ^\\d\\d\\s[A-Za-z]{1,20}$

我还注意到您使用 for 循环 for(String str : result){ 将每一列与正则表达式进行匹配。在我看来,您应该只匹配 result[1]pkgAddr

最后一点,请查看Regex 101 。它将允许您针对一堆输入测试正则表达式,看看它们是否匹配。

关于java - 使用模式匹配从文件中排序,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54412473/

相关文章:

python - 如何在 Python 中不使用循环和 join() 将列表转换为字符串

java.lang.NoSuchFieldError : INSTANCE Exception While Running Appium Android Test

java - JTabbedPane:第一次打开之前具有不同颜色的新选项卡

javascript - 正则表达式提取括号中的单词不起作用

C++ 组合 2 个数组,数据有序

javascript - 按隐藏输入对表进行排序

java - '"python"无法识别'错误,但仅当从 Java 运行时

java - Oracle sql随机数种子

Java Regex - 从 xml 中排除空标签

regex - 使用可选参数重写规则以在 .htaccess 中进行重定向