Example usage for java.util.regex Pattern matcher

List of usage examples for java.util.regex Pattern matcher

Introduction

In this page you can find the example usage for java.util.regex Pattern matcher.

Prototype

public Matcher matcher(CharSequence input) 

Source Link

Document

Creates a matcher that will match the given input against this pattern.

Usage

From source file:uk.ac.ebi.intact.editor.controller.misc.MyNotesController.java

public static void main(String[] args) {
    Pattern p = Pattern.compile("\\{(\\w+):(\\w+)\\s(.+)\\}");

    Matcher matcher = p.matcher(
            "{query:Lalalala select exp from Experiment exp where exp.bioSource.cvTissue.ac = 'EBI-2609142'}");

    while (matcher.find()) {
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));
    }/*w w w  . j  ava 2 s. com*/
}

From source file:AIR.Common.Web.PatternUrlRewriter.java

public static void main(String[] args) {
    try {//from   w  ww . j  a v  a2  s .  co  m
        Pattern p = Pattern.compile(
                "file:///D:/DataFiles/BB_Files/tds2_airws_org/TDSCore_2013-2014/Bank-([^/]*)/Items/Item-(?<bankid>[^/]*)-(?<itemid>[^/]*)/([^/]*)");

        Matcher m = p.matcher(
                "file:///D:/DataFiles/BB_Files/tds2_airws_org/TDSCore_2013-2014/Bank-179/Items/Item-195-6821/Item_22489_v5_rubric.xml");

        URL url = new URL(m.replaceFirst(
                "file:///C:/AIROSE Trainer Deployments/tmp/Datafiles/Item-${bankid}-${itemid}/Item_${itemid}_v5_rubric.xml"));
        System.err.println(url);
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}

From source file:de.mpg.escidoc.services.cone.util.CCCrawler.java

public static void main(String[] args) throws Exception {
    HttpClient httpClient = new HttpClient();

    querier = QuerierFactory.newQuerier(false);

    // field_commercial
    for (YesNo fieldCommercial : YesNo.values()) {
        // field_derivatives
        for (YesNo fieldDerivatives : YesNo.values()) {
            // field_derivatives
            for (Jurisdiction fieldJurisdiction : Jurisdiction.values()) {
                String licenceUrl = ccUrl + "&field_commercial=" + fieldCommercial.toString()
                        + "&field_derivatives=" + fieldDerivatives.toString() + "&field_jurisdiction="
                        + fieldJurisdiction.toString() + "&lang=de_DE";
                System.out.println(licenceUrl);
                GetMethod method = new GetMethod(licenceUrl);
                ProxyHelper.executeMethod(httpClient, method);

                if (method.getStatusCode() == 200) {
                    TreeFragment fragment = new TreeFragment();

                    String key1 = "urn:cone:commercial";
                    String key3 = "urn:cone:jurisdiction";

                    List<LocalizedTripleObject> list = new ArrayList<LocalizedTripleObject>();
                    list.add(new LocalizedString(fieldCommercial.toBoolean()));
                    fragment.put(key1, list);

                    List<LocalizedTripleObject> list2 = new ArrayList<LocalizedTripleObject>();
                    list2.add(new LocalizedString(fieldDerivatives.toBoolean()));
                    fragment.put("urn:cone:derivatives", list2);

                    List<LocalizedTripleObject> list3 = new ArrayList<LocalizedTripleObject>();
                    list3.add(new LocalizedString(fieldJurisdiction.toString()));
                    fragment.put(key3, list3);

                    String codeToCopy = extractCode(method);

                    Pattern urlPattern = Pattern.compile("href=\"([^\"]+)\"");
                    Matcher urlMatcher = urlPattern.matcher(codeToCopy);
                    if (urlMatcher.find()) {
                        String url = urlMatcher.group(1);
                        fragment.setSubject(url);

                        Pattern versionPattern = Pattern.compile("/(\\d+\\.\\d+)/[^/]+/$");
                        Matcher versionMatcher = versionPattern.matcher(url);
                        if (versionMatcher.find()) {
                            list = new ArrayList<LocalizedTripleObject>();
                            list.add(new LocalizedString(versionMatcher.group(1)));
                            fragment.put("urn:cone:version", list);
                        }/* w w  w  .  j a v a 2s .c om*/

                        Pattern imgPattern = Pattern.compile("src=\"([^\"]+)\"");
                        Matcher imgMatcher = imgPattern.matcher(codeToCopy);
                        if (imgMatcher.find()) {
                            list = new ArrayList<LocalizedTripleObject>();
                            list.add(new LocalizedString(imgMatcher.group(1)));
                            fragment.put("http://xmlns.com/foaf/0.1/depiction", list);
                        }

                        GetMethod method2 = new GetMethod(url);
                        ProxyHelper.executeMethod(httpClient, method2);
                        String page = method2.getResponseBodyAsString();

                        Pattern namePattern = Pattern.compile("<h2 property=\"dc:title\">([^<]+)</h2>");
                        Matcher nameMatcher = namePattern.matcher(page);
                        if (nameMatcher.find()) {
                            list = new ArrayList<LocalizedTripleObject>();
                            list.add(new LocalizedString(nameMatcher.group(1)));
                            fragment.put("http://purl.org/dc/elements/1.1/title", list);
                        }

                        List<LocalizedTripleObject> languages = extractLanguages(page, url);

                        fragment.put("urn:cone:translation", languages);

                        querier.delete("cclicences", url);
                        querier.create("cclicences", url, fragment);
                    }

                } else {
                    System.out.println("Not found: " + licenceUrl);
                }
            }
        }
    }
    querier.release();
}

From source file:it.unipd.dei.ims.falcon.CmdLine.java

public static void main(String[] args) {

    // last argument is always index path
    Options options = new Options();
    // one of these actions has to be specified
    OptionGroup actionGroup = new OptionGroup();
    actionGroup.addOption(new Option("i", true, "perform indexing")); // if dir, all files, else only one file
    actionGroup.addOption(new Option("q", true, "perform a single query"));
    actionGroup.addOption(new Option("b", false, "perform a query batch (read from stdin)"));
    actionGroup.setRequired(true);/*  ww  w.jav a2s.c o  m*/
    options.addOptionGroup(actionGroup);

    // other options
    options.addOption(new Option("l", "segment-length", true, "length of a segment (# of chroma vectors)"));
    options.addOption(
            new Option("o", "segment-overlap", true, "overlap portion of a segment (# of chroma vectors)"));
    options.addOption(new Option("Q", "quantization-level", true, "quantization level for chroma vectors"));
    options.addOption(new Option("k", "min-kurtosis", true, "minimum kurtosis for indexing chroma vectors"));
    options.addOption(new Option("s", "sub-sampling", true, "sub-sampling of chroma features"));
    options.addOption(new Option("v", "verbose", false, "verbose output (including timing info)"));
    options.addOption(new Option("T", "transposition-estimator-strategy", true,
            "parametrization for the transposition estimator strategy"));
    options.addOption(new Option("t", "n-transp", true,
            "number of transposition; if not specified, no transposition is performed"));
    options.addOption(new Option("f", "force-transp", true, "force transposition by an amount of semitones"));
    options.addOption(new Option("p", "pruning", false,
            "enable query pruning; if -P is unspecified, use default strategy"));
    options.addOption(new Option("P", "pruning-custom", true, "custom query pruning strategy"));

    // parse
    HelpFormatter formatter = new HelpFormatter();
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
        if (cmd.getArgs().length != 1)
            throw new ParseException("no index path was specified");
    } catch (ParseException ex) {
        System.err.println("ERROR - parsing command line:");
        System.err.println(ex.getMessage());
        formatter.printHelp("falcon -{i,q,b} [options] index_path", options);
        return;
    }

    // default values
    final float[] DEFAULT_TRANSPOSITION_ESTIMATOR_STRATEGY = new float[] { 0.65192807f, 0.0f, 0.0f, 0.0f,
            0.3532628f, 0.4997167f, 0.0f, 0.41703504f, 0.0f, 0.16297342f, 0.0f, 0.0f };
    final String DEFAULT_QUERY_PRUNING_STRATEGY = "ntf:0.340765*[0.001694,0.995720];ndf:0.344143*[0.007224,0.997113];"
            + "ncf:0.338766*[0.001601,0.995038];nmf:0.331577*[0.002352,0.997884];"; // TODO not the final one

    int hashes_per_segment = Integer.parseInt(cmd.getOptionValue("l", "150"));
    int overlap_per_segment = Integer.parseInt(cmd.getOptionValue("o", "50"));
    int nranks = Integer.parseInt(cmd.getOptionValue("Q", "3"));
    int subsampling = Integer.parseInt(cmd.getOptionValue("s", "1"));
    double minkurtosis = Float.parseFloat(cmd.getOptionValue("k", "-100."));
    boolean verbose = cmd.hasOption("v");
    int ntransp = Integer.parseInt(cmd.getOptionValue("t", "1"));
    TranspositionEstimator tpe = null;
    if (cmd.hasOption("t")) {
        if (cmd.hasOption("T")) {
            // TODO this if branch is yet to test
            Pattern p = Pattern.compile("\\d\\.\\d*");
            LinkedList<Double> tokens = new LinkedList<Double>();
            Matcher m = p.matcher(cmd.getOptionValue("T"));
            while (m.find())
                tokens.addLast(new Double(cmd.getOptionValue("T").substring(m.start(), m.end())));
            float[] strategy = new float[tokens.size()];
            if (strategy.length != 12) {
                System.err.println("invalid transposition estimator strategy");
                System.exit(1);
            }
            for (int i = 0; i < strategy.length; i++)
                strategy[i] = new Float(tokens.pollFirst());
        } else {
            tpe = new TranspositionEstimator(DEFAULT_TRANSPOSITION_ESTIMATOR_STRATEGY);
        }
    } else if (cmd.hasOption("f")) {
        int[] transps = parseIntArray(cmd.getOptionValue("f"));
        tpe = new ForcedTranspositionEstimator(transps);
        ntransp = transps.length;
    }
    QueryPruningStrategy qpe = null;
    if (cmd.hasOption("p")) {
        if (cmd.hasOption("P")) {
            qpe = new StaticQueryPruningStrategy(cmd.getOptionValue("P"));
        } else {
            qpe = new StaticQueryPruningStrategy(DEFAULT_QUERY_PRUNING_STRATEGY);
        }
    }

    // action
    if (cmd.hasOption("i")) {
        try {
            Indexing.index(new File(cmd.getOptionValue("i")), new File(cmd.getArgs()[0]), hashes_per_segment,
                    overlap_per_segment, subsampling, nranks, minkurtosis, tpe, verbose);
        } catch (IndexingException ex) {
            Logger.getLogger(CmdLine.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CmdLine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (cmd.hasOption("q")) {
        String queryfilepath = cmd.getOptionValue("q");
        doQuery(cmd, queryfilepath, hashes_per_segment, overlap_per_segment, nranks, subsampling, tpe, ntransp,
                minkurtosis, qpe, verbose);
    }
    if (cmd.hasOption("b")) {
        try {
            long starttime = System.currentTimeMillis();
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String line = null;
            while ((line = in.readLine()) != null && !line.trim().isEmpty())
                doQuery(cmd, line, hashes_per_segment, overlap_per_segment, nranks, subsampling, tpe, ntransp,
                        minkurtosis, qpe, verbose);
            in.close();
            long endtime = System.currentTimeMillis();
            System.out.println(String.format("total time: %ds", (endtime - starttime) / 1000));
        } catch (IOException ex) {
            Logger.getLogger(CmdLine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:ReaderIter.java

public static void main(String[] args) throws IOException {
    // The RE pattern
    Pattern patt = Pattern.compile("[A-Za-z][a-z]+");
    // A FileReader (see the I/O chapter)
    BufferedReader r = new BufferedReader(new FileReader("ReaderIter.java"));

    // For each line of input, try matching in it.
    String line;/*from w w  w .  ja v a2  s. c  o  m*/
    while ((line = r.readLine()) != null) {
        // For each match in the line, extract and print it.
        Matcher m = patt.matcher(line);
        while (m.find()) {
            // Simplest method:
            // System.out.println(m.group(0));

            // Get the starting position of the text
            int start = m.start(0);
            // Get ending position
            int end = m.end(0);
            // Print whatever matched.
            // Use CharacterIterator.substring(offset, end);
            System.out.println(line.substring(start, end));
        }
    }
}

From source file:RegExpExample.java

public static void main(String args[]) {
    String fileName = "RETestSource.java";

    String unadornedClassRE = "^\\s*class (\\w+)";
    String doubleIdentifierRE = "\\b(\\w+)\\s+\\1\\b";

    Pattern classPattern = Pattern.compile(unadornedClassRE);
    Pattern doublePattern = Pattern.compile(doubleIdentifierRE);
    Matcher classMatcher, doubleMatcher;

    int lineNumber = 0;

    try {//  w  ww .  j a v a 2s.  c  o m
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;

        while ((line = br.readLine()) != null) {
            lineNumber++;

            classMatcher = classPattern.matcher(line);
            doubleMatcher = doublePattern.matcher(line);

            if (classMatcher.find()) {
                System.out.println("The class [" + classMatcher.group(1) + "] is not public");
            }

            while (doubleMatcher.find()) {
                System.out.println("The word \"" + doubleMatcher.group(1) + "\" occurs twice at position "
                        + doubleMatcher.start() + " on line " + lineNumber);
            }
        }
    } catch (IOException ioe) {
        System.out.println("IOException: " + ioe);
        ioe.printStackTrace();
    }
}

From source file:com.gzj.tulip.jade.statement.SystemInterpreter.java

public static void main(String[] args) throws Exception {
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("table", "my_table_name");
    parameters.put("id", "my_id");
    parameters.put(":1", "first_param");

    final Pattern PATTERN = Pattern.compile("\\{([a-zA-Z0-9_\\.\\:]+)\\}|##\\((.+)\\)");

    String sql = "select form ##(:table) {name} where {id}='{1}'";

    StringBuilder sb = new StringBuilder(sql.length() + 200);
    Matcher matcher = PATTERN.matcher(sql);
    int start = 0;
    while (matcher.find(start)) {
        sb.append(sql.substring(start, matcher.start()));
        String group = matcher.group();
        String key = null;//from   www  .j a v a2 s . com
        if (group.startsWith("{")) {
            key = matcher.group(1);
        } else if (group.startsWith("##(")) {
            key = matcher.group(2);
        }
        System.out.println(key);
        if (key == null || key.length() == 0) {
            continue;
        }
        Object value = parameters.get(key); // {paramName}?{:1}?
        if (value == null) {
            if (key.startsWith(":") || key.startsWith("$")) {
                value = parameters.get(key.substring(1)); // {:paramName}
            } else {
                char ch = key.charAt(0);
                if (ch >= '0' && ch <= '9') {
                    value = parameters.get(":" + key); // {1}?
                }
            }
        }
        if (value == null) {
            value = parameters.get(key); // ?
        }
        if (value != null) {
            sb.append(value);
        } else {
            sb.append(group);
        }
        start = matcher.end();
    }
    sb.append(sql.substring(start));
    System.out.println(sb);

}

From source file:RegExTest.java

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter pattern: ");
    String patternString = in.nextLine();

    Pattern pattern = null;
    try {//w  w w.ja v a 2s.  co  m
        pattern = Pattern.compile(patternString);
    } catch (PatternSyntaxException e) {
        System.out.println("Pattern syntax error");
        System.exit(1);
    }

    while (true) {
        System.out.println("Enter string to match: ");
        String input = in.nextLine();
        if (input == null || input.equals(""))
            return;
        Matcher matcher = pattern.matcher(input);
        if (matcher.matches()) {
            System.out.println("Match");
            int g = matcher.groupCount();
            if (g > 0) {
                for (int i = 0; i < input.length(); i++) {
                    for (int j = 1; j <= g; j++)
                        if (i == matcher.start(j))
                            System.out.print('(');
                    System.out.print(input.charAt(i));
                    for (int j = 1; j <= g; j++)
                        if (i + 1 == matcher.end(j))
                            System.out.print(')');
                }
                System.out.println();
            }
        } else
            System.out.println("No match");
    }
}

From source file:org.apache.asterix.experiment.client.LSMExperimentSetRunner.java

public static void main(String[] args) throws Exception {
    java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(Level.FINEST);
    //        LogManager.getRootLogger().setLevel(org.apache.log4j.Level.OFF);
    LSMExperimentSetRunnerConfig config = new LSMExperimentSetRunnerConfig(
            String.valueOf(System.currentTimeMillis()), 3);
    CmdLineParser clp = new CmdLineParser(config);
    try {/*from   w w  w.j  a  v  a 2  s .  co  m*/
        clp.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        clp.printUsage(System.err);
        System.exit(1);
    }

    final String pkg = "org.apache.asterix.experiment.builder.suite";
    Reflections reflections = //new Reflections("org.apache.asterix.experiment.builder.suite");
            new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(pkg))
                    .filterInputsBy(new FilterBuilder().includePackage(pkg))
                    .setScanners(new TypeElementsScanner().publicOnly(), new MethodParameterScanner()));
    Map<String, AbstractLSMBaseExperimentBuilder> nameMap = new TreeMap<>();
    for (Constructor c : reflections.getConstructorsMatchParams(LSMExperimentSetRunnerConfig.class)) {
        AbstractLSMBaseExperimentBuilder b = (AbstractLSMBaseExperimentBuilder) c.newInstance(config);
        nameMap.put(b.getName(), b);
    }

    Pattern p = config.getRegex() == null ? null : Pattern.compile(config.getRegex());

    SequentialActionList exps = new SequentialActionList();
    for (Map.Entry<String, AbstractLSMBaseExperimentBuilder> e : nameMap.entrySet()) {
        if (p == null || p.matcher(e.getKey()).matches()) {
            exps.add(e.getValue().build());
            if (LOGGER.isLoggable(Level.INFO)) {
                LOGGER.info("Added " + e.getKey() + " to run list...");
            }
        }
    }
    exps.perform();
}

From source file:BookRank.java

/** Grab the sales rank off the web page and log it. */
public static void main(String[] args) throws Exception {

    Properties p = new Properties();
    String title = p.getProperty("title", "NO TITLE IN PROPERTIES");
    // The url must have the "isbn=" at the very end, or otherwise
    // be amenable to being string-catted to, like the default.
    String url = p.getProperty("url", "http://test.ing/test.cgi?isbn=");
    // The 10-digit ISBN for the book.
    String isbn = p.getProperty("isbn", "0000000000");
    // The RE pattern (MUST have ONE capture group for the number)
    String pattern = p.getProperty("pattern", "Rank: (\\d+)");

    // Looking for something like this in the input:
    //    <b>QuickBookShop.web Sales Rank: </b>
    //    26,252//w  w  w  .j ava2 s . com
    //    </font><br>

    Pattern r = Pattern.compile(pattern);

    // Open the URL and get a Reader from it.
    BufferedReader is = new BufferedReader(new InputStreamReader(new URL(url + isbn).openStream()));
    // Read the URL looking for the rank information, as
    // a single long string, so can match RE across multi-lines.
    String input = "input from console";
    // System.out.println(input);

    // If found, append to sales data file.
    Matcher m = r.matcher(input);
    if (m.find()) {
        PrintWriter pw = new PrintWriter(new FileWriter(DATA_FILE, true));
        String date = // `date +'%m %d %H %M %S %Y'`;
                new SimpleDateFormat("MM dd hh mm ss yyyy ").format(new Date());
        // Paren 1 is the digits (and maybe ','s) that matched; remove comma
        Matcher noComma = Pattern.compile(",").matcher(m.group(1));
        pw.println(date + noComma.replaceAll(""));
        pw.close();
    } else {
        System.err.println("WARNING: pattern `" + pattern + "' did not match in `" + url + isbn + "'!");
    }

    // Whether current data found or not, draw the graph, using
    // external plotting program against all historical data.
    // Could use gnuplot, R, any other math/graph program.
    // Better yet: use one of the Java plotting APIs.

    String gnuplot_cmd = "set term png\n" + "set output \"" + GRAPH_FILE + "\"\n" + "set xdata time\n"
            + "set ylabel \"Book sales rank\"\n" + "set bmargin 3\n" + "set logscale y\n"
            + "set yrange [1:60000] reverse\n" + "set timefmt \"%m %d %H %M %S %Y\"\n" + "plot \"" + DATA_FILE
            + "\" using 1:7 title \"" + title + "\" with lines\n";

    Process proc = Runtime.getRuntime().exec("/usr/local/bin/gnuplot");
    PrintWriter gp = new PrintWriter(proc.getOutputStream());
    gp.print(gnuplot_cmd);
    gp.close();
}