正则表达式搜索序列并首先停止“,”

时间:2018-03-09 11:16:32

标签: java regex

在下列情况下我需要一些正则表达式的帮助。 我正在读一个包含多个文件的文件夹,如A.AAA2000.XYZ或B.BBB2000.AY

我必须在每个文件中搜索一行(或多行),其格式如下:

CALL(或CALL-PROC或ENTER)$ XX.whatever,whatever1,whatever2等等。 XX.w可以是我文件夹中的另一个文件,或者甚至不存在。我需要做的是查看哪些文件包含该模式,并且在该模式中,如果那些XX.whatever是文件或不存在并输出那些不存在的模式。问题是我必须在第一次出现时停止“,”否则我得到错误的结果,我似乎无法让它正常工作。我做了一切,除了摆脱那个“,”。我在下面附上了一些代码和示例,如果可以,请提供帮助:

示例(按预期工作):

Searching file A.AAA2000.XYZ 
Found procedure(s): $XX.B.BBB.2000.AY,LALA,LALA1,LALA2


Searching file B.BBB.2000.AY
Found procedure(s): $XX.C.CCC.2000.XYZ,LALALA,LALALALA,LALALALA


Searching file C.CCC.2000.XYZ
ERROR: File doesn't exist or no procedures called


Procedures found: 
B.BBB.2000.AY
Procedures not found: 
C.CCC.2000.XYZ

示例2(它现在如何工作):

Searching file A.AAA2000.XYZ
Found procedure(s): #XX.B.BBB.2000.AY,LALA,LALA1,LALA2


Searching file B.BBB.2000.AY
Found procedure(s): #XX.C.CCC.2000.XYZ,LALALA,LALALALA,LALALALA


Searching file C.CCC.2000.XYZ
ERROR: File doesn't exist or no procedures called


...........................

...........................

...........................

Procedures found: 
XX.C.CCC.2000.XYZ,LALALA,LALALALA,LALALALALA        
Procedures not found: 
B.BBB.2000.AY,LALA,LALA1,LALA2
C.CCC.2000.XYZ,LALALA,LALALALA,LALALALA

部分代码:

private static final String[] _keyWords = {"CALL-PROC", "CALL", "ENTER"};
private static final String _procedureRegex = ".* \\$PR\\..*";
private static final String _lineSkipper = "/REMARK";
private static final String _procedureNameFormat = "\\$PR\\..+";

 private static boolean CallsProcedure(String givenLine) 

    {for (String keyWord : _keyWords) {
        if (givenLine.contains(keyWord) && !givenLine.contains(_lineSkipper)) {
            Pattern procedurePattern = Pattern.compile(_procedureRegex);
            Matcher procedureMatcher = procedurePattern.matcher(givenLine);


            return procedureMatcher.find();
        }
    }

读:

 private void ReadContent(File givenFile,
                         HashMap<String, HashSet<String>> whereToAddProcedures,
                         HashMap<String, HashSet<String>> whereToAddFiles) throws IOException {
    System.out.println("Processing file " + givenFile.getAbsolutePath());
    BufferedReader fileReader = new BufferedReader(new FileReader(givenFile));
    String currentLine;


    while ((currentLine = fileReader.readLine()) != null) {
        if (CallsProcedure(currentLine)) {

            String CProc = currentLine.split("\\$PR\\.")[1];


            if (whereToAddProcedures.containsKey(CProc)) {

                System.out.println("Procedure " + CProc + " already exists, adding more paths.");

                whereToAddProcedures.get(CProc).add(givenFile.getAbsolutePath());
            } else {
                System.out.println("Adding Procedure " + CProc);

                whereToAddProcedures.put(CProc,
                        new HashSet<>(Collections.singletonList(givenFile.getAbsolutePath())));
            }

            if (givenFile.getName().matches(_procedureNameFormat)) {
                if (whereToAddFiles.containsKey(givenFile.getAbsolutePath())) {
                    System.out.println("File " + givenFile.getName()
                            + " already has procedure calls, adding " + CProc);

                    whereToAddProcedures.get(givenFile.getName()).add(CProc);
                } else {
                    System.out.println("Adding Procedure Call for " + CProc + " to "
                            + givenFile.getName());

                    whereToAddProcedures.put(givenFile.getName(),
                            new HashSet<>(Collections.singletonList(CProc)));
                }
            }
        }
    }

    fileReader.close();

1 个答案:

答案 0 :(得分:0)

如果逗号是模式结尾的标记,则可以将其作为正则表达式的最后位置,在逗号出现时停止匹配。喜欢这个

_procedureRegex = ".* \\$PR\\.[^,]*";