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:Main.java

private static Object getValue(Object o, String fieldName) {
    try {//from  ww  w. j  a  v a2 s  . c o m
        Object value = o;
        for (String exp : fieldName.split("\\.")) {
            Method m;
            if (exp.endsWith("()")) {
                m = value.getClass().getMethod(exp.substring(0, exp.length() - 2));
            } else if (ARRAY_REF.matcher(exp).matches()) {
                Matcher matcher = ARRAY_REF.matcher(exp);
                matcher.find();
                String methodNameBody = getMethodNameBody(matcher.group(1));
                int arrayIndex = Integer.parseInt(matcher.group(2));
                value = ((Object[]) value.getClass().getMethod("get" + methodNameBody)
                        .invoke(value))[arrayIndex];
                m = null;
            } else {
                String methodNameBody = getMethodNameBody(exp);
                try {
                    m = value.getClass().getMethod("get" + methodNameBody);
                } catch (NoSuchMethodException ex) {
                    m = value.getClass().getMethod("is" + methodNameBody);
                }
            }
            if (m != null)
                value = m.invoke(value);
        }
        return value;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

/**
 * Handle various common charset name errors, and return something
 * that will be considered valid (and is normalized)
 * //from ww w.  j a va  2s.  c om
 * @param charsetName name of charset to process
 * @return potentially remapped/cleaned up version of charset name
 */
public static String clean(String charsetName) {
    if (charsetName == null) {
        return null;
    }

    // Get rid of cruft around names, like <>, trailing commas, etc.
    Matcher m = CHARSET_NAME_PATTERN.matcher(charsetName);
    if (!m.matches()) {
        return null;
    }

    String result = m.group(1);
    if (CHARSET_ALIASES.containsKey(result.toLowerCase())) {
        // Handle common erroneous charset names.
        result = CHARSET_ALIASES.get(result.toLowerCase());
    } else if (ISO_NAME_PATTERN.matcher(result).matches()) {
        // Handle "iso 8859-x" error
        m = ISO_NAME_PATTERN.matcher(result);
        m.matches();
        result = "iso-8859-" + m.group(1);
    } else if (CP_NAME_PATTERN.matcher(result).matches()) {
        // Handle "cp-xxx" error
        m = CP_NAME_PATTERN.matcher(result);
        m.matches();
        result = "cp" + m.group(1);
    } else if (WIN_NAME_PATTERN.matcher(result).matches()) {
        // Handle "winxxx" and "win-xxx" errors
        m = WIN_NAME_PATTERN.matcher(result);
        m.matches();
        result = "windows-" + m.group(2);
    }

    try {
        Charset cs = Charset.forName(result);
        return cs.name();
    } catch (Exception e) {
        return null;
    }
}

From source file:dk.netarkivet.common.utils.DomainUtils.java

/** Return a domain name.
 * A domain name is defined as either an IP address if the given host is an
 * IP address, or a postfix of the given host name containing one
 * hostnamepart and a TLD as defined in settings.
 *
 * E.g. if '.dk' and 'co.uk' are valid TLDs, www.netarchive.dk will be
 * become netarchive.dk and news.bbc.co.uk will be come bbc.co.uk
 *
 * @param hostname A hostname or IP address. Null hostname is not allowed
 * @return A domain name (foo.bar) or IP address, or null if no valid
 * domain could be obtained from the given hostname.  If non-null,
 * the return value is guaranteed to be a valid domain as determined
 * by isValidDomainName()./*from  w  w w  . java  2s.  c o  m*/
 */
public static String domainNameFromHostname(String hostname) {
    ArgumentNotValid.checkNotNull(hostname, "String hostname");
    String result = hostname;
    // IP addresses are kept as-is, others are trimmed down.
    if (!Constants.IP_KEY_REGEXP.matcher(hostname).matches()) {
        Matcher matcher = HOSTNAME_REGEX.matcher(hostname);
        if (matcher.matches()) {
            result = matcher.group(2);
        }
    }
    if (isValidDomainName(result)) {
        return result;
    }
    return null;
}

From source file:org.jitsi.meet.test.util.JvbUtil.java

static private int getConferenceCount(CharSequence jvbStatsJson) {
    int conferenceCount = -1;

    Pattern confPattern = Pattern.compile("\"conferences\": +\"?((\\d+))\"?");

    Matcher confMatcher = confPattern.matcher(jvbStatsJson);

    if (confMatcher.find()) {
        String countTxt = confMatcher.group(1);

        return Integer.parseInt(countTxt);
    }/*from w ww.  j ava 2s  . c o m*/

    return conferenceCount;
}

From source file:Main.java

/**
 * Parse <xmlRoot name="this.is.name" value="this.is.value"> and return name, value
 *
 * @param xmlRoot xml tag name/*from  w w  w . j  a va 2 s  .  co  m*/
 * @param xmlStr string containing xml
 * @return String[]{name, value}
 */
public static String[] getNameValue(String xmlRoot, String xmlStr) {
    String[] strs = new String[] { "", "" };
    Matcher m;
    m = Pattern
            .compile("<" + xmlRoot + " name=\"(.*?)\" value=\"(.*?)\" />", Pattern.MULTILINE | Pattern.DOTALL)
            .matcher(xmlStr);
    if (m.find()) {
        strs[0] = m.group(1);
        strs[1] = m.group(2);
    }
    return strs;
}

From source file:com.machinepublishers.jbrowserdriver.Util.java

static String charset(URLConnection conn) {
    String charset = conn.getContentType();
    if (charset != null) {
        Matcher matcher = charsetPattern.matcher(charset);
        if (matcher.find()) {
            charset = matcher.group(1);
            if (Charset.isSupported(charset)) {
                return charset;
            }//from ww w .j ava2s  .  c o  m
        }
    }
    return "utf-8";
}

From source file:com.beyondjservlet.gateway.servlet.support.ProxySupport.java

/**
 * Returns the contents of a {@code Set-Cookie} header with it's optional {@code path} and
 * {@code domain} attributes replaced by the passed in values.
 *
 * @param header the {@code Set-Cookie} header for which {@code path} and {@code domain} should be replaced.
 * @param path the new path for the cookie. If null the original value of for path is used.
 * @param domain the domain for the cookie. If null the original value of for domain is used.
 * @return {@code String} the contents of the {@code Set-Cookie} header. The reason for not returning the complete
 *                        header including the headers name (Set-Cookie) is make this method useful with the
 *                        Java Servlet API and {@link HttpServletResponse#setHeader(String, String)}.
 *//*w w w . j  a  va  2s.com*/
public static String replaceCookieAttributes(final String header, final String path, final String domain) {
    final Matcher m = PATH_AND_DOMAIN_PATTERN.matcher(header);
    final StringBuffer sb = new StringBuffer();
    while (m.find()) {
        final String name = m.group(1);
        if ("domain".equalsIgnoreCase(name)) {
            appendReplacement(m, sb, name, domain);
        } else if ("path".equalsIgnoreCase(name)) {
            appendReplacement(m, sb, name, path);
        }
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:com.amazonaws.services.dynamodbv2.streams.connectors.DynamoDBConnectorUtilities.java

/**
 * Convert a given DynamoDB endpoint into its corresponding Streams endpoint
 *
 * @param endpoint/*from   w w  w .ja  va2s.c  om*/
 *            given endpoint URL
 * @return the extracted Streams endpoint corresponding to the given DynamoDB endpoint
 */
public static String getStreamsEndpoint(String endpoint) {
    String regex = DynamoDBConnectorConstants.PROTOCOL_REGEX;
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(endpoint);
    String ret;
    if (matcher.matches()) {
        ret = ((matcher.group(1) == null) ? "" : matcher.group(1)) + DynamoDBConnectorConstants.STREAMS_PREFIX
                + matcher.group(2);
    } else {
        ret = DynamoDBConnectorConstants.STREAMS_PREFIX + endpoint;
    }
    return ret;
}

From source file:com.ejisto.core.classloading.util.ReflectionUtils.java

public static String cleanGenericSignature(String signature) {
    String genericSignature = signature.trim();
    Matcher m = JAVASSIST_GENERIC_SIGNATURE.matcher(genericSignature);
    if (m.matches()) {
        genericSignature = m.group(1);
    }/*from  w  ww.j a  v a2s.  co  m*/
    m = GENERIC_ELEMENT_EXTRACTOR.matcher(genericSignature);
    List<String> result = new ArrayList<>();
    String match;
    while (m.find()) {
        match = m.group(3);
        if (isNotBlank(match)) {
            result.add(m.group(3));
        }
    }
    return join(result, ", ");
}

From source file:de.thischwa.pmcms.tool.image.Dimension.java

/**
 * Grab the {@link Dimension} from the attributes of an img-tag. If height/width attribute doesn't exists, the style attribute will be
 * scanned.//from  w  w  w .jav a 2s . c o m
 * 
 * @param attr {@link Map} of the attributes of the img-tag. The key is the name of the attribute.
 * @return {@link Dimension} grabbed from the attributes of an img-tag.
 * @throws IllegalArgumentException If the {@link Dimension} can't grab from the attributes. 
 */
public static Dimension getDimensionFromAttr(Map<String, String> attr) throws IllegalArgumentException {
    String heightStr = attr.get("height");
    String widthStr = attr.get("width");
    if (heightStr != null && widthStr != null)
        return new Dimension(Integer.parseInt(widthStr), Integer.parseInt(heightStr));

    if (StringUtils.isBlank(attr.get("style")))
        throw new IllegalArgumentException("Couldn't get the dimension from img-tag attributes!");
    String style = StringUtils.replace(attr.get("style"), " ", "");
    int height = -1;
    int width = -1;

    Matcher matcher = styleHeightPattern.matcher(style);
    if (matcher.matches())
        height = Integer.parseInt(matcher.group(1));

    matcher = styleWidthPattern.matcher(style);
    if (matcher.matches())
        width = Integer.parseInt(matcher.group(1));

    if (width == -1 || height == -1)
        throw new IllegalArgumentException("Couldn't get the dimension from style-attribute!");
    return new Dimension(width, height);
}