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

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

使用模式匹配从文件排序,Java,java,regex,sorting,pattern-matching,filereader,Java,Regex,Sorting,Pattern Matching,Filereader,因此,我已经让我的程序能够正确地分离文本文件的行,甚至可以匹配第一行文本的模式,但我还需要能够检测和分离文本文件的地址行,并根据它们的方向或街道/百老汇对它们进行排序,但我甚至无法获得初始模式无法检测到地址设置。我是否使用了错误的正则表达式,这就是地址部分无法正确检测的原因吗 代码 package csi311; // Import some standard Java libraries. import java.io.BufferedReader; import java.io.FileR

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

代码

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();
        }    
    }
}
下面是应该能够处理地址行的代码行,但由于某些原因,它与模式和正确分隔地址行的输出不匹配,可以在处理地址的for循环上方的print语句中看到,但由于某些原因,地址行甚至没有被检测为匹配和im我不明白为什么会这样

代码行问题与

  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
,但也将匹配
abc
bac
abracadabra
等。为了避免这种情况,必须使用锚定符号
^
$
分别设置开始和结束。然后正则表达式变成
^\\d\\d\\s[A-Za-z]{1,20}$

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


最后一点,请看。它将允许您根据一组输入测试正则表达式,以查看它们是否匹配。

为什么不使用逗号拆分或使用适当的CSV解析器?这里可能不需要正则表达式。行在逗号上拆分,并单独打印,但我需要能够匹配t的模式他将行拆分,然后将它们分成特定的部分,我可以在arraylist中这样做,但问题是由于某种原因,地址部分的模式没有被检测到,我不明白为什么我读到的所有关于正则表达式和格式的内容都表明我对它的格式是正确的用于检测文件中文本第一部分的匹配模式的ine。这和正则表达式的使用是必需的。例如,
456-BGT-9876 22百老汇24 678-FGH-9846 45第五大道12.2 347-poy-3465 101 B'way 24
是打印出来的,我需要能够匹配
22百老汇
部分的模式。使用正则表达式我甚至制作了一个正则表达式我需要能够检测到这个匹配,因为我需要调整它,以便能够检测到
百老汇
部分,然后将其添加到arraylist中,作为转到
百老汇
所需的传递。您可以添加所需的输出吗提出问题?我不完全理解你在寻找什么。完成,这是我试图从文本文件中获取的输出,在第一个逗号之后,这是交付的地址部分,我需要模式匹配这些地址,并能够打印出来。一旦我能得到地址匹配的点ed然后会担心匹配到百老汇的特定模式。非常感谢!修复了我的正则表达式问题,我对它的工作原理有了更好的了解,走吧我的程序中没有几个小问题,但没有什么我不能解决的。
22 Broadway
45 5th Ave
101 B'way