Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Match a pattern with a single capturing group and return the content of
     * the capturing group
     *
     * @param text    the text to match against
     * @param pattern the pattern (regular expression) must contain one and only one
     *                capturing group
     * @return
     */
    public static String matchPattern(String text, String pattern) {
        // Use regular expression matching to pull the min and max values from
        // the output of gdalinfo
        Pattern p1 = Pattern.compile(pattern, Pattern.MULTILINE);
        Matcher m1 = p1.matcher(text);
        if (m1.find()) {
            if (m1.groupCount() == 1) {
                return m1.group(1);
            } else {
                throw new RuntimeException("error matching pattern " + pattern);
            }
        } else {
            throw new RuntimeException("error matching pattern " + pattern);
        }
    }
}