繁体   English   中英

多组正则表达式问题

[英]Regex Issue With Multiple Groups

我正在尝试创建一个正则表达式模式以匹配以下格式的行:

field[bii] = float4:.4f_degree  // Galactic Latitude
field[class] = int2  (index) // Browse Object Classification
field[dec] = float8:.4f_degree (key) // Declination
field[name] = char20  (index) // Object Designation
field[dircos1] = float8   // 1st Directional Cosine

我想出了这种模式,该模式似乎有效,然后突然似乎不起作用:

field\[(.*)\] = (float|int|char)([0-9]|[1-9][0-9]).*(:(\.([0-9])))

这是我要使用的代码(编辑:提供完整方法而不是摘录):

private static Map<String, String> createColumnMap(String filename) {

    // create a linked hashmap mapping field names to their column types. Use LHM because I'm picky and
    // would prefer to preserve the order
    Map<String, String> columnMap = new LinkedHashMap<String, String>();

    // define the regex patterns
    Pattern columnNamePattern = Pattern.compile(columnNameRegexPattern);

    try {
        Scanner scanner = new Scanner(new FileInputStream(filename));
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();

            if (line.indexOf("field[") != -1) {
                // get the field name
                Matcher fieldNameMatcher = columnNamePattern.matcher(line);
                String fieldName = null;
                if (fieldNameMatcher.find()) {
                    fieldName = fieldNameMatcher.group(1);
                }

                String columnName = null;
                String columnType = null;
                String columnPrecision = null;
                String columnScale = null;
                //Pattern columnTypePattern = Pattern.compile(".*(float|int|char)([0-9]|[1-9][0-9])");
                Pattern columnTypePattern = Pattern.compile("field\\[(.*)\\] = (float|int|char).*([0-9]|[1-9][0-9]).*(:(\\.([0-9])))");
                Matcher columnTypeMatcher = columnTypePattern.matcher(line);

                System.out.println(columnTypeMatcher.lookingAt());

                if (columnTypeMatcher.lookingAt()) {
                    System.out.println(fieldName + ": " + columnTypeMatcher.groupCount());
                    int count = columnTypeMatcher.groupCount();
                    if (count > 1) {
                        columnName = columnTypeMatcher.group(1);
                        columnType = columnTypeMatcher.group(2);
                    }
                    if (count > 2) {
                        columnScale = columnTypeMatcher.group(3);
                    }
                    if (count >= 6) {
                        columnPrecision = columnTypeMatcher.group(6);
                    }
                }

                int precision = Integer.parseInt(columnPrecision);
                int scale = Integer.parseInt(columnScale);

                if (columnType.equals("int")) {
                    if (precision <= 4) {
                        columnMap.put(fieldName, "INTEGER");
                    } else {
                        columnMap.put(fieldName, "BIGINT");
                    }
                } else if (columnType.equals("float")) {
                    if (columnPrecision==null) {
                        columnMap.put(fieldName,"DECIMAL(8,4)");
                    } else {
                        columnMap.put(fieldName,"DECIMAL(" + columnPrecision + "," + columnScale + ")");
                    }
                } else {
                    columnMap.put(fieldName,"VARCHAR("+columnPrecision+")");
                }
            }

            if (line.indexOf("<DATA>") != -1) {
                scanner.close();
                break;
            }
        }

        scanner.close();
    } catch (FileNotFoundException e) {

    }

    return columnMap;
}

当我从Matcher对象获得groupCount时,它说有6个组。 但是,它们与文本不匹配,因此我绝对可以使用一些帮助...任何人都可以提供帮助?

我不清楚您要做什么,但我想出了以下模式,它接受了您所有的输入示例:

field\\[(.*)\\] = (float|int|char)([1-9][0-9]?)?(:\\.([0-9]))?

使用此代码:

    String columnName = null;
    String columnType = null;
    String columnPrecision = null;
    String columnScale = null;
    // Pattern columnTypePattern =
    // Pattern.compile(".*(float|int|char)([0-9]|[1-9][0-9])");
    // field\[(.*)\] = (float|int|char)([0-9]|[1-9][0-9]).*(:(\.([0-9])))
    Pattern columnTypePattern = Pattern
            .compile("field\\[(.*)\\] = (float|int|char)([1-9][0-9]?)?(:\\.([0-9]))?");
    Matcher columnTypeMatcher = columnTypePattern.matcher(line);

    boolean match = columnTypeMatcher.lookingAt();
    System.out.println("Match: " + match);

    if (match) {
        int count = columnTypeMatcher.groupCount();
        if (count > 1) {
            columnName = columnTypeMatcher.group(1);
            columnType = columnTypeMatcher.group(2);
        }
        if (count > 2) {
            columnScale = columnTypeMatcher.group(3);
        }
        if (count > 4) {
            columnPrecision = columnTypeMatcher.group(5);
        }
        System.out.println("Name=" + columnName + "; Type=" + columnType + "; Scale=" + columnScale + "; Precision=" + columnPrecision);
    }

我认为您的正则表达式的问题是需要使比例和精度为可选。

 field\\[(.*)\\] = (float|int|char)([0-9]|[1-9][0-9]).*(:(\\.([0-9]))) 

.*过于宽泛,而且有很多冗余的([0-9]|[1-9][0-9])我认为开头的括号组:和之前的.*应可选的。

消除所有歧义后,我得到

field\[([^\]]*)\] = (float|int|char)(0|[1-9][0-9]+)(?:[^:]*(:(\.([0-9]+))))?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM