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:mitm.application.djigzo.james.mailets.SpecialAddress.java

public static SpecialAddress fromName(String name) {
    name = StringUtils.trimToNull(name);

    if (name != null) {
        Matcher matcher = PATTERN.matcher(name);

        if (matcher.matches()) {
            name = StringUtils.trimToNull(matcher.group(1));

            for (SpecialAddress specialAddress : SpecialAddress.values()) {
                if (specialAddress.name.equalsIgnoreCase(name)) {
                    return specialAddress;
                }/*from w  w  w  .  ja v a 2s  .c  o  m*/
            }
        }
    }

    return null;
}

From source file:com.magnet.plugin.r2m.helpers.UrlParser.java

/**
 * @param url url where path param are expanded (removed "{""}")
 * @return expanded url//  w ww  .  j a v  a 2 s  . co  m
 */
public static String expandUrl(String url) {
    Matcher m = PATH_PARAM_PATTERN.matcher(url);
    while (m.find()) {
        String paramDef = m.group(1);
        String[] paramParts = paramDef.split(":");
        if (paramParts.length > 1) {
            url = url.replaceAll(R2MConstants.START_TEMPLATE_VARIABLE_REGEX + paramParts[0]
                    + R2MConstants.END_TEMPLATE_VARIABLE_REGEX, paramParts[1]);
        } else {
            url = url.replaceAll(R2MConstants.START_TEMPLATE_VARIABLE_REGEX + paramParts[0]
                    + R2MConstants.END_TEMPLATE_VARIABLE_REGEX, paramParts[0]);
        }
    }

    return url;
}

From source file:com.diyshirt.util.RegexUtil.java

/**
 * Return the specified match "groups" from the pattern.
 * For each group matched a String will be entered in the ArrayList.
 *
 * @param pattern The Pattern to use.//  w w w  .jav  a 2s .  c  om
 * @param match The String to match against.
 * @param group The group number to return in case of a match.
 * @return
 */
public static ArrayList getMatches(Pattern pattern, String match, int group) {
    ArrayList matches = new ArrayList();
    Matcher matcher = pattern.matcher(match);
    while (matcher.find()) {
        matches.add(matcher.group(group));
    }
    return matches;
}

From source file:nya.miku.wishmaster.http.recaptcha.RecaptchaNoscript.java

static String getChallenge(String publicKey, CancellableTask task, HttpClient httpClient, String scheme)
        throws Exception {
    if (scheme == null)
        scheme = "http";
    String response = HttpStreamer.getInstance().getStringFromUrl(scheme + RECAPTCHA_CHALLENGE_URL + publicKey,
            HttpRequestModel.builder().setGET().build(), httpClient, null, task, false);
    Matcher matcher = CHALLENGE_FIRST_PATTERN.matcher(response);
    if (matcher.find()) {
        String challenge = matcher.group(1);
        try {//from  w  w w .  j  a  v  a 2 s .c om
            response = HttpStreamer.getInstance().getStringFromUrl(
                    scheme + RECAPTCHA_RELOAD_URL + challenge + "&k=" + publicKey,
                    HttpRequestModel.builder().setGET().build(), httpClient, null, task, false);
            matcher = CHALLENGE_RELOAD_PATTERN.matcher(response);
            if (matcher.find()) {
                String newChallenge = matcher.group(1);
                if (newChallenge != null && newChallenge.length() > 0)
                    return newChallenge;
            }
        } catch (Exception e) {
            Logger.e(TAG, e);
        }

        return challenge;
    }
    throw new RecaptchaException("can't parse recaptcha challenge answer");
}

From source file:Main.java

public static String getRootTag(String xmlStr) {
    Matcher m;
    m = Pattern.compile("^[\r\n \t]*<.*?>[\r\n \t]*<(.*?)>", Pattern.MULTILINE | Pattern.DOTALL)
            .matcher(xmlStr);/*from www .j a v  a2 s  .  c  om*/
    if (m.find()) {
        return m.group(1);
    }
    return "";
}

From source file:edu.harvard.i2b2.fhirserver.ws.I2b2Helper.java

static String extractPatientId(String input) {
    if (input == null)
        return null;
    String id = null;/*from ww w .j av  a2  s. c o  m*/
    Pattern p = Pattern.compile("[Subject:subject|Patient|patient|_id]=([a-zA-Z0-9]+)");
    Matcher m = p.matcher(input);

    if (m.find()) {
        id = m.group(1);
        logger.trace(id);
    }
    return id;
}

From source file:com.linkedin.databus.core.util.StringUtils.java

/**
 * Strip username/password information from the JDBC DB uri to be used for logging
 * @param uri     the JDBC URI to sanitize
 * @return the sanitized DB URI/*from   w ww.ja v  a2s. co  m*/
 */
public static String sanitizeDbUri(String uri) {
    String result = uri;
    Matcher m = ORA_JDBC_URI_PATTERN.matcher(uri);
    if (m.matches()) {
        result = m.group(1) + "*/*" + m.group(4);
    } else if (uri.startsWith("jdbc:mysql:")) {
        Matcher m1 = MYSQL_JDBC_PATTERN1.matcher(result);
        Matcher m2 = MYSQL_JDBC_PATTERN2.matcher(result);
        if (m1.find()) {
            result = m1.replaceAll("($1*)");
        } else if (m2.find()) {
            result = m2.replaceAll("$1*");
        }
    }

    return result;
}

From source file:edu.infsci2560.LoginHelper.java

private static HttpHeaders getHeaders(TestRestTemplate template, String route) {
    // todo : write getHeaders test
    HttpHeaders headers = new HttpHeaders();
    ResponseEntity<String> page = template.getForEntity(route, String.class);
    assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK);

    String cookie = page.getHeaders().getFirst("Set-Cookie");
    headers.set("Cookie", cookie);

    Pattern pattern = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*");
    Matcher matcher = pattern.matcher(page.getBody());
    assertThat(matcher.matches()).as(page.getBody()).isTrue();
    headers.set("X-CSRF-TOKEN", matcher.group(1));
    return headers;
}

From source file:br.pub.tradutor.TradutorController.java

/**
 * Mtodo utilizado para traduo utilizando o Google Translate
 *
 * @param text Texto a ser traduzido//from   w  ww  .j a va  2  s.  c o m
 * @param from Idioma de origem
 * @param to Idioma de destino
 * @return Texto traduzido (no idioma destino)
 * @throws IOException
 */
public static String translate(String text, String from, String to) throws IOException {
    //Faz encode de URL, para fazer escape dos valores que vo na URL
    String encodedText = URLEncoder.encode(text, "UTF-8");

    DefaultHttpClient httpclient = new DefaultHttpClient();

    //Mtodo GET a ser executado
    HttpGet httpget = new HttpGet(String.format(TRANSLATOR, encodedText, from, to));

    //Faz a execuo
    HttpResponse response = httpclient.execute(httpget);

    //Busca a resposta da requisio e armazena em String
    String returnContent = EntityUtils.toString(response.getEntity());

    //Desconsidera tudo depois do primeiro array
    returnContent = returnContent.split("\\],\\[")[0];

    //StringBuilder que sera carregado o retorno
    StringBuilder translatedText = new StringBuilder();

    //Verifica todas as tradues encontradas, e junta todos os trechos
    Matcher m = RESULTS_PATTERN.matcher(returnContent);
    while (m.find()) {
        translatedText.append(m.group(1).trim()).append(' ');
    }

    //Retorna
    return translatedText.toString().trim();

}

From source file:com.alu.e3.prov.lifecycle.IDHelper.java

/**
 * Extract datas from the given filename.
 * @param fileName/*www. jav  a2 s.  c o  m*/
 * @return a String[] containing respectively: { apiIDEncoded, apiID, provID }
 */
public static String[] extractAllFromFileName(String fileName) {
    Pattern p = Pattern.compile("([a-f0-9]+)-([a-f0-9]+)\\.[a-z]{3}");
    Matcher m = p.matcher(fileName);
    if (!m.matches())
        return null;

    String apiIDEncoded = m.group(1);
    String apiID = null;
    try {
        apiID = IDHelper.decode(apiIDEncoded);
    } catch (RuntimeException e) {
        e.printStackTrace();
        if (logger.isErrorEnabled()) {
            logger.error("Undecodable apiId:{}", apiIDEncoded);
        }
    }
    String provID = m.group(2);

    String[] datas = new String[] { apiIDEncoded, apiID, provID };

    return datas;
}