Example usage for java.util.regex Matcher group

List of usage examples for java.util.regex Matcher group

Introduction

In this page you can find the example usage for java.util.regex Matcher group.

Prototype

public String group(String name) 

Source Link

Document

Returns the input subsequence captured by the given named-capturing group during the previous match operation.

Usage

From source file:com.opengamma.bbg.loader.BloombergSwaptionFileLoader.java

/**
 * Little util to parse swaption tickers into a csv for further analysis.
 * @param args command line params/*w w w .  ja v a  2  s. c  o m*/
 */
public static void main(String[] args) { // CSIGNORE
    CSVReader csvReader = null;
    CSVWriter csvWriter = null;
    try {
        csvReader = new CSVReader(new BufferedReader(new FileReader(args[0])));
        csvWriter = new CSVWriter(new BufferedWriter(new FileWriter(args[1])));
        String[] line;
        Pattern pattern = Pattern.compile("^(\\w\\w\\w).*?(\\d+)(M|Y)(\\d+)(M|Y)\\s*?(PY|RC)\\s*?(.*)$");
        BloombergReferenceDataProvider rawBbgRefDataProvider = getBloombergSecurityFileLoader();
        MongoDBValueCachingReferenceDataProvider bbgRefDataProvider = MongoCachedReferenceData
                .makeMongoProvider(rawBbgRefDataProvider, BloombergSwaptionFileLoader.class);
        while ((line = csvReader.readNext()) != null) {
            String name = line[NAME_FIELD];
            Matcher matcher = pattern.matcher(name);
            if (matcher.matches()) {
                String ccy = matcher.group(1);
                String swapTenorSize = matcher.group(2);
                String swapTenorUnit = matcher.group(3);
                String optionTenorSize = matcher.group(4);
                String optionTenorUnit = matcher.group(5);
                String payReceive = matcher.group(6);
                String distanceATM = matcher.group(7);

                String buid = "/buid/" + line[BUID_FIELD];
                String value = bbgRefDataProvider.getReferenceDataValue(buid, "TICKER");
                csvWriter.writeNext(new String[] { name, ccy, swapTenorSize, swapTenorUnit, optionTenorSize,
                        optionTenorUnit, payReceive, distanceATM, value });
            } else {
                s_logger.error("Couldn't parse " + name + " field");
            }

        }
    } catch (IOException ioe) {
        s_logger.error("Error while reading file", ioe);
    } finally {
        IOUtils.closeQuietly(csvReader);
        IOUtils.closeQuietly(csvWriter);
    }
}

From source file:com.xue777hua.emails.test.Test.java

public static void main(String[] args) {
    String text = "<div id=\"frag_1\" class=\"page_fragment auth_frag\" data-first=\"true\" data-fid=\"1\"><div class=\"module_topic_paths\"></div><h1 class=\"svTitle\" id=\"tm005\">Effect of inulin and pectin on rheological and thermal properties of potato starch paste and gel</h1><ul class=\"authorGroup noCollab\"><li><a href=\"#\" class=\"authorName\" id=\"authname_N41d730a0N3ee493d4\" data-t=\"a\" data-fn=\"Teresa\" data-ln=\"Witczak\" data-pos=\"1\" data-tb=\"\">Teresa Witczak</a><a title=\"Affiliation: a\" href=\"#af005\" class=\"intra_ref auth_aff\" id=\"baf005\"><sup>a</sup></a><sup>, </sup><a title=\"Corresponding author contact information\" href=\"#cor1\" id=\"bcor1\" class=\"intra_ref auth_corr\"><img class=\"imgLazyJSB\" alt=\"Corresponding author contact information\" src=\"/sd/grey_pxl.gif\" data-inlimg=\"/entities/REcor.gif\"><noscript><img alt=\"Corresponding author contact information\" src=\"http://origin-cdn.els-cdn.com/sd/entities/REcor.gif\"></noscript></a><sup>, </sup><a href=\"mailto:t.witczak@ur.krakow.pl\" class=\"auth_mail\"><img class=\"imgLazyJSB\" src=\"/sd/grey_pxl.gif\" alt=\"E-mail the corresponding author\" data-inlimg=\"/entities/REemail.gif\"><noscript><img src=\"http://origin-cdn.els-cdn.com/sd/entities/REemail.gif\" alt=\"E-mail the corresponding author\"></noscript></a>, </li><li><a href=\"#\" class=\"authorName\" id=\"authname_N41d730a0N3ee4953c\" data-t=\"a\" data-fn=\"Mariusz\" data-ln=\"Witczak\" data-pos=\"2\" data-tb=\"\">Mariusz Witczak</a><a title=\"Affiliation: a\" href=\"#af005\" class=\"intra_ref auth_aff\" id=\"baf005\"><sup>a</sup></a>, </li><li><a href=\"#\" class=\"authorName\" id=\"authname_N41d730a0N3ee495f0\" data-t=\"a\" data-fn=\"Rafa&#322;\" data-ln=\"Ziobro\" data-pos=\"3\" data-tb=\"\">Rafa Ziobro</a><a title=\"Affiliation: b\" href=\"#af010\" class=\"intra_ref auth_aff\" id=\"baf010\"><sup>b</sup></a></li></ul><!--VALIDHTML--><ul class=\"affiliation\"><li id=\"af005\"><sup>a</sup> <span id=\"\">Department of Engineering and Machinery for Food Industry, University of Agriculture in Krakow, Balicka 122 Str., 30-149 Krakow, Poland</span></li><li id=\"af010\"><sup>b</sup> <span id=\"\">Department of Carbohydrates Technology, University of Agriculture in Krakow, Balicka 122 Str., 30-149 Krakow, Poland</span></li></ul><!--VALIDHTML--><!--VALIDHTML--><dl class=\"articleDates\"><dd>Received 24 May 2013, Revised 1 October 2013, Accepted 1 October 2013, Available online 11 October 2013</dd></dl><!--VALIDHTML--><div class=\"moreInformation\"></div><div id=\"ppvPlaceHolder\" class=\"hidden\"></div><!--VALIDHTML--><div id=\"showMoreButtons\"></div><dl class=\"extLinks\"><dd class=\"doiLink\"></dd><dd class=\"rightsLink\"></dd></dl><div class=\"articleOAlabelForced\"></div><div id=\"refersToAndreferredToBy\"><dl id=\"referredToBy\" class=\"documentThread\"><!--Referred To By--></dl></div><!--FRAGMENTEND--><div class=\"page_fragment_ind auth_frag\" data-id=\"frag_2\"></div></div>";
    String authorList = "";
    String articleTitle = "";
    // ???title/*  w  w w  . j a  va  2 s . c o m*/
    Pattern articleTitlePattern = Pattern.compile("<h1.+?svTitle.+?>(.+?)</h1>");
    Matcher articleTitleMatcher = articleTitlePattern.matcher(text);
    while (articleTitleMatcher.find()) {
        articleTitle = articleTitleMatcher.group(1);
        articleTitle = StringEscapeUtils.unescapeHtml(articleTitle);
        articleTitle = StringUtils.stripTags(articleTitle);
        System.out.println("" + articleTitle);
    }
    // ???
    Pattern p = Pattern.compile("<ul.+?authorGroup.+?>(.+?)</ul>");
    Matcher m = p.matcher(text);
    while (m.find()) {
        authorList = m.group(1);
    }
    p = Pattern.compile("<li>(.+?)</li>");
    m = p.matcher(authorList);
    while (m.find()) {
        String authorItem = m.group(1);
        if (authorItem.contains("mailto")) {
            Pattern nameEmailPattern = Pattern
                    .compile("data-tb=\"[\\d]{0,}\">(.+?)</a>.*href=\"mailto:(.+?)\" class=\"auth_mail\">");
            Matcher nameEmailMatcher = nameEmailPattern.matcher(authorItem);
            if (nameEmailMatcher.find()) {
                String name = nameEmailMatcher.group(1);
                String email = nameEmailMatcher.group(2);
                name = StringEscapeUtils.unescapeHtml(name);
                // ???new ArrayList
                List<String> fieldList = new ArrayList<String>();
                fieldList.add(name);
                fieldList.add(email);
                fieldList.add(articleTitle);
                int hashKey = (name + email + articleTitle).hashCode();
                System.out.println("?????TitlehashKey:" + name + "|" + email
                        + "|" + articleTitle + "|" + hashKey + ",  nameEmailsList");

            }
        }
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    String inputStr = "abbabcd";
    String patternStr = "(a(?:b*))+(c*)";
    // (?:b*) is a non-capturing group

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();

    if (matchFound) {
        for (int i = 0; i <= matcher.groupCount(); i++) {
            String groupStr = matcher.group(i);
        }//  ww w  . jav a 2 s .c o m
    }
}

From source file:Main.java

public static void main(String[] asd) {
    String sourcestring = "source string to match with pattern";
    Pattern re = Pattern.compile("[^ABC\\s+]");
    Matcher m = re.matcher(sourcestring);
    int mIdx = 0;
    while (m.find()) {
        for (int groupIdx = 0; groupIdx < m.groupCount() + 1; groupIdx++) {
            System.out.println("[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
        }//from w  w  w  .ja va2  s  .c om
        mIdx++;
    }
}

From source file:com.rslakra.java.TestUrlConnection.java

public static void main(String[] args) {
    String urlString = "https://devamatre.com/";
    HttpResponse httpResponse = HTTPHelper.executeGetRequest(urlString, null, true);
    System.out.println(httpResponse.getRequestHeaders());

    String formActionValue = extractFormActionValue(httpResponse.getDataBytes());
    System.out.println("\nformActionValue:\n" + formActionValue);

    String dataString = new String(httpResponse.getDataBytes());
    Pattern pattern = Pattern.compile("\"");
    Matcher matcher = pattern.matcher(dataString);
    if (matcher.matches()) {
        System.out.println("Matched\n");
        System.out.println(matcher.group(1));
    }/*from  www  .j  a v a2s.co m*/

    System.out.println(StringEscapeUtils.unescapeHtml(urlString));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "abbabcd";
    String patternStr = "(a(b*))+(c*)";

    // Compile and use regular expression
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();

    if (matchFound) {
        // Get all groups for this match
        for (int i = 0; i <= matcher.groupCount(); i++) {
            String groupStr = matcher.group(i);
        }/*w  ww  .  j a v  a2 s.  c o m*/
    }
}

From source file:com.ariatemplates.seleniumjavarobot.Main.java

public static void main(String[] args) throws Exception {
    SeleniumJavaRobot seleniumJavaRobot = new SeleniumJavaRobot();
    String browser;/*from  ww  w.ja  v  a2s  .com*/
    seleniumJavaRobot.autoRestart = false;
    if (OS.isFamilyMac()) {
        browser = "safari";
    } else {
        browser = "firefox";
    }
    seleniumJavaRobot.url = "http://localhost:7777/__attester__/slave.html";
    String usageString = String.format(
            "Usage: selenium-java-robot [options]\nOptions:\n  --auto-restart\n  --url <url> [default: %s]\n  --browser <browser> [default: %s, accepted values: %s]\n  -DpropertyName=value",
            seleniumJavaRobot.url, browser, BROWSERS_LIST.toString());
    for (int i = 0, l = args.length; i < l; i++) {
        String curParam = args[i];
        if ("--browser".equalsIgnoreCase(curParam) && i + 1 < l) {
            browser = args[i + 1];
            i++;
        } else if ("--url".equalsIgnoreCase(curParam) && i + 1 < l) {
            seleniumJavaRobot.url = args[i + 1];
            i++;
        } else if ("--auto-restart".equalsIgnoreCase(curParam)) {
            seleniumJavaRobot.autoRestart = true;
        } else if ("--version".equalsIgnoreCase(curParam)) {
            System.out.println(Main.class.getPackage().getImplementationVersion());
            return;
        } else if ("--help".equalsIgnoreCase(curParam)) {
            System.out.println(usageString);
            return;
        } else {
            Matcher matcher = SET_SYSTEM_PROPERTY_REGEXP.matcher(curParam);
            if (matcher.matches()) {
                System.setProperty(matcher.group(1), matcher.group(2));
            } else {
                System.err.println("Unknown command line option: " + curParam);
                System.err.println(usageString);
                return;
            }
        }
    }
    seleniumJavaRobot.robotizedBrowserFactory = LocalRobotizedBrowserFactory
            .createRobotizedWebDriverFactory(browser);
    seleniumJavaRobot.start();
    closeOnStreamEnd(seleniumJavaRobot, System.in);
    closeOnProcessEnd(seleniumJavaRobot);
}

From source file:eu.annocultor.converters.geonames.GeonamesDumpToRdf.java

public static void main(String[] args) throws Exception {
    File root = new File("input_source");

    // load country-continent match
    countryToContinent//from w  ww  .ja va2 s .  c  o  m
            .load((new GeonamesDumpToRdf()).getClass().getResourceAsStream("/country-to-continent.properties"));

    // creating files
    Map<String, BufferedWriter> files = new HashMap<String, BufferedWriter>();
    Map<String, Boolean> started = new HashMap<String, Boolean>();

    for (Object string : countryToContinent.keySet()) {
        String continent = countryToContinent.getProperty(string.toString());
        File dir = new File(root, continent);
        if (!dir.exists()) {
            dir.mkdir();
        }
        files.put(string.toString(), new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(new File(root, continent + "/" + string + ".rdf")), "UTF-8")));
        System.out.println(continent + "/" + string + ".rdf");
        started.put(string.toString(), false);
    }

    System.out.println(started);

    Pattern countryPattern = Pattern
            .compile("<inCountry rdf\\:resource\\=\"http\\://www\\.geonames\\.org/countries/\\#(\\w\\w)\"/>");
    long counter = 0;
    LineIterator it = FileUtils.lineIterator(new File(root, "all-geonames-rdf.txt"), "UTF-8");
    try {
        while (it.hasNext()) {
            String text = it.nextLine();
            if (text.startsWith("http://sws.geonames"))
                continue;

            // progress
            counter++;
            if (counter % 100000 == 0) {
                System.out.print("*");
            }
            //         System.out.println(counter);
            // get country
            String country = null;
            Matcher matcher = countryPattern.matcher(text);
            if (matcher.find()) {
                country = matcher.group(1);
            }
            //         System.out.println(country);
            if (country == null)
                country = "null";
            text = text.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><rdf:RDF",
                    "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><rdf:RDF");
            if (started.get(country) == null)
                throw new Exception("Unknow country " + country);
            if (started.get(country).booleanValue()) {
                // remove RDF opening
                text = text.substring(text.indexOf("<rdf:RDF "));
                text = text.substring(text.indexOf(">") + 1);
            }
            // remove RDF ending
            text = text.substring(0, text.indexOf("</rdf:RDF>"));
            files.get(country).append(text + "\n");
            if (!started.get(country).booleanValue()) {
                // System.out.println("Started with country " + country);
            }
            started.put(country, true);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }

    for (Object string : countryToContinent.keySet()) {
        boolean hasStarted = started.get(string.toString()).booleanValue();
        if (hasStarted) {
            BufferedWriter bf = files.get(string.toString());
            bf.append("</rdf:RDF>");
            bf.flush();
            bf.close();
        }
    }
    return;
}

From source file:gobblin.util.CLIPasswordEncryptor.java

public static void main(String[] args) throws ParseException {
    CommandLine cl = parseArgs(args);//from   ww w .  jav  a2s.com
    if (shouldPrintUsageAndExit(cl)) {
        printUsage();
        return;
    }
    String masterPassword = getMasterPassword(cl);
    TextEncryptor encryptor = getEncryptor(cl, masterPassword);

    if (cl.hasOption(ENCRYPTED_PWD_OPTION)) {
        Matcher matcher = ENCRYPTED_PATTERN.matcher(cl.getOptionValue(ENCRYPTED_PWD_OPTION));
        if (matcher.find()) {
            String encrypted = matcher.group(1);
            System.out.println(encryptor.decrypt(encrypted));
        } else {
            throw new RuntimeException("Input encrypted password does not match pattern \"ENC(...)\"");
        }
    } else if (cl.hasOption(PLAIN_PWD_OPTION)) {
        System.out.println("ENC(" + encryptor.encrypt(cl.getOptionValue(PLAIN_PWD_OPTION)) + ")");
    } else {
        printUsage();
        throw new RuntimeException(
                String.format("Must provide -%s or -%s option.", PLAIN_PWD_OPTION, ENCRYPTED_PWD_OPTION));
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    CharSequence inputStr = "abbabcd";
    String patternStr = "(a(b*))+(c*)";

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.find();

    if (matchFound) {
        // Get all groups for this match
        for (int i = 0; i <= matcher.groupCount(); i++) {
            // Get the group's captured text
            String groupStr = matcher.group(i);

            // Get the group's indices
            int groupStart = matcher.start(i);
            int groupEnd = matcher.end(i);

            // groupStr is equivalent to
            inputStr.subSequence(groupStart, groupEnd);
        }/*from   w  ww . ja va 2 s.  c o m*/
    }
}