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:net.jadler.stubbing.server.jdk.RequestUtils.java

static void addEncoding(final Request.Builder builder, final HttpExchange httpExchange) {
    final String contentType = httpExchange.getRequestHeaders().getFirst("Content-Type");
    if (contentType != null) {
        final Matcher matcher = CHARSET_PATTERN.matcher(contentType);
        if (matcher.matches()) {
            try {
                builder.encoding(Charset.forName(matcher.group(1)));
            } catch (UnsupportedCharsetException e) {
                //just ignore, fallback encoding will be used instead
            }/*from   w  ww  .j a  va 2  s .  c  o m*/
        }
    }
}

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

public static String encodeEmail(String str) {
    // obfuscate mailto's: turns them into hex encoded,
    // so that browsers can still understand the mailto link
    Matcher mailtoMatch = mailtoPattern.matcher(str);
    while (mailtoMatch.find()) {
        String email = mailtoMatch.group(1);
        //System.out.println("email=" + email);
        String hexed = encode(email);
        str = str.replaceFirst("mailto:" + email, "mailto:" + hexed);
    }//from   w w  w  . ja v  a  2 s  .c  o m

    return obfuscateEmail(str);
}

From source file:de.snertlab.xdccBee.irc.DccMessageParser.java

public static DccPacket buildDccPacket(String sender, String dccMessage) {
    DccPacket dccPacket = new DccPacket();
    dccPacket.setSender(sender);/*from   w  w  w  .  ja  va  2 s . com*/

    String s = cleanMessage(dccMessage);
    Matcher m = DCC_MESSAGE_PATTER.matcher(s);
    if (m.find()) {
        dccPacket.setPacketNr(Integer.parseInt(StringUtils.trim(m.group(1))));
        dccPacket.setSize(StringUtils.trim(m.group(2)));
        String packetName = m.group(3);
        packetName = StringUtils.trim(packetName);
        dccPacket.setName(packetName);
    }
    return dccPacket;
}

From source file:com.linkedin.databus2.schemas.FileSystemVersionedSchemaSetProvider.java

static VersionedSchemaId parseSchemaVersion(String fileName) throws IOException {
    Matcher matcher = FILE_NAME_PATTERN.matcher(fileName);
    if (matcher.matches()) {
        String baseName = matcher.group(1);
        short version = Short.parseShort(matcher.group(2));
        return new VersionedSchemaId(baseName, version);
    } else {/*  ww  w  .j a  va 2 s  .com*/
        logger.warn("Invalid file name: " + fileName);
        return null;
    }
}

From source file:com.thoughtworks.go.server.newsecurity.utils.BasicAuthHeaderExtractor.java

public static UsernamePassword extractBasicAuthenticationCredentials(String authorizationHeader) {
    if (isBlank(authorizationHeader)) {
        return null;
    }// w w w  .  j a v  a  2 s . c o  m

    final Matcher matcher = BASIC_AUTH_EXTRACTOR_PATTERN.matcher(authorizationHeader);
    if (matcher.matches()) {
        final String encodedCredentials = matcher.group(1);
        final byte[] decode = Base64.getDecoder().decode(encodedCredentials);
        String decodedCredentials = new String(decode, StandardCharsets.UTF_8);

        final int indexOfSeparator = decodedCredentials.indexOf(':');
        if (indexOfSeparator == -1) {
            throw new BadCredentialsException("Invalid basic authentication credentials specified in request.");
        }

        final String username = decodedCredentials.substring(0, indexOfSeparator);
        final String password = decodedCredentials.substring(indexOfSeparator + 1);

        return new UsernamePassword(username, password);
    }

    return null;
}

From source file:Utils.java

public static List<String> getFound(String contents, String regex) {
    if (isEmpty(regex) || isEmpty(contents)) {
        return null;
    }//w w  w .ja  v  a2 s .c o m
    List<String> results = new ArrayList<String>();
    Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE);
    Matcher matcher = pattern.matcher(contents);

    while (matcher.find()) {
        if (matcher.groupCount() > 0) {
            results.add(matcher.group(1));
        } else {
            results.add(matcher.group());
        }
    }
    return results;
}

From source file:com.android.dialer.lookup.gebeld.GebeldApi.java

public static ContactInfo reverseLookup(Context context, String number) throws IOException {
    String phoneNumber = number.replace("+31", "0");
    Uri uri = Uri.parse(REVERSE_LOOKUP_URL).buildUpon().appendQueryParameter("queryfield1", phoneNumber)
            .build();/*from   ww  w .j  a v a  2  s. c om*/
    // Cut out everything we're not interested in (scripts etc.) to
    // speed up the subsequent matching.
    String output = LookupUtils.firstRegexResult(LookupUtils.httpGet(new HttpGet(uri.toString())),
            "<div class=\"small-12 large-4 columns information\">(.*?)</div>", true);

    String name = null;
    String address = null;

    if (output == null) {
        return null;
    } else {
        Pattern pattern = Pattern.compile(REGEX, 0);
        Matcher m = pattern.matcher(output);

        if (m.find()) {
            name = LookupUtils.fromHtml(m.group(1).trim());

            if (m.find()) {
                address = LookupUtils.fromHtml(m.group(1).trim());

                if (m.find()) {
                    address += "\n" + LookupUtils.fromHtml(m.group(1).trim());
                }
            }
        }
    }

    if (name == null) {
        return null;
    }

    ContactInfo info = new ContactInfo();
    info.name = name;
    info.address = address;
    info.formattedNumber = phoneNumber != null ? phoneNumber : number;
    info.website = uri.toString();

    return info;
}

From source file:net.pms.formats.v2.AudioAttribute.java

public static AudioAttribute getAudioAttributeByLibMediaInfoKeyValuePair(String keyValuePair) {
    if (isBlank(keyValuePair)) {
        throw new IllegalArgumentException("Empty keyValuePair passed in.");
    }//from w w w.  ja va2  s  . com

    Matcher keyMatcher = libMediaInfoKeyPattern.matcher(keyValuePair);
    if (keyMatcher.find()) {
        String key = keyMatcher.group(1);
        AudioAttribute audioAttribute = libMediaInfoKeyToAudioAttributeMap.get(key.toLowerCase());
        if (audioAttribute == null) {
            throw new IllegalArgumentException("Can't find AudioAttribute for key '" + key + "'.");
        } else {
            return audioAttribute;
        }
    } else {
        throw new IllegalArgumentException("Can't find key in keyValuePair '" + keyValuePair + "'.");
    }
}

From source file:Main.java

/**
 * Identify variables in the given string and replace with their values as
 * defined in the variableDefs map.  Variables without definitions are left
 * alone.//www .  j  av  a2s  .  c  o  m
 *
 * @param string String to do variable replacement in.
 * @param variableDefs Map from variable names to values.
 * @return modified string
 */
public static String replaceVariablesInString(String string, Map<String, String> variableDefs) {

    StringBuffer replacementStringBuffer = new StringBuffer();

    Matcher variableMatcher = variablePattern.matcher(string);
    while (variableMatcher.find()) {
        String varString = variableMatcher.group(1).trim();
        int eqIdx = varString.indexOf("=");

        String varName;
        if (eqIdx > -1)
            varName = varString.substring(0, eqIdx).trim();
        else
            varName = varString;

        String replacementString = variableDefs.containsKey(varName) ? variableDefs.get(varName)
                : variableMatcher.group(0);

        variableMatcher.appendReplacement(replacementStringBuffer, Matcher.quoteReplacement(replacementString));
    }
    variableMatcher.appendTail(replacementStringBuffer);

    return replacementStringBuffer.toString();
}

From source file:com.github.fritaly.svngraph.Utils.java

public static String getBranchPath(String path) {
    final Pattern pattern = Pattern.compile("(.*/branches/([^/]+))(/.*)?");

    final Matcher matcher = pattern.matcher(path);

    return matcher.matches() ? matcher.group(1) : null;
}