Example usage for java.util.regex Matcher groupCount

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

Introduction

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

Prototype

public int groupCount() 

Source Link

Document

Returns the number of capturing groups in this matcher's pattern.

Usage

From source file:com.aliyun.openservices.odps.console.resource.ListFunctionsCommand.java

public static ListFunctionsCommand parse(String commandString, ExecutionContext sessionContext)
        throws ODPSConsoleException {

    Matcher match = PATTERN.matcher(commandString);
    Matcher pubMatch = PUBLIC_PATTERN.matcher(commandString);

    if (match.matches()) {
        return new ListFunctionsCommand(commandString, sessionContext, null);
    } else if (pubMatch.matches()) {
        String project = null;/* ww w. ja  v a2  s.c o  m*/

        if (3 == pubMatch.groupCount() && pubMatch.group(3) != null) {
            CommandLine cl = getCommandLine(pubMatch.group(3));
            if (0 != cl.getArgs().length) {
                throw new ODPSConsoleException(ODPSConsoleConstants.BAD_COMMAND + "[invalid paras]");
            }
            project = cl.getOptionValue("p");
        }
        return new ListFunctionsCommand(commandString, sessionContext, project);
    }

    return null;
}

From source file:org.apache.hadoop.hbase.regionserver.StoreFileInfo.java

/**
 * @param name file name to check./*  ww  w. j  a  v a  2  s .  c o  m*/
 * @return True if the path has format of a HStoreFile reference.
 */
public static boolean isReference(final String name) {
    Matcher m = REF_NAME_PATTERN.matcher(name);
    return m.matches() && m.groupCount() > 1;
}

From source file:com.tmall.wireless.tangram.util.Utils.java

/**
 * <pre>/*from   w w  w .  j a  v  a2s .co m*/
 * parse image ratio from imageurl with regex as follows:
 * (\d+)-(\d+)(_?q\d+)?(\.[jpg|png|gif])
 * (\d+)x(\d+)(_?q\d+)?(\.[jpg|png|gif])
 *
 * samples urls:
 * http://img.alicdn.com/tps/i1/TB1x623LVXXXXXZXFXXzo_ZPXXX-372-441.png --> return 372, 441
 * http://img.alicdn.com/tps/i1/TB1P9AdLVXXXXa_XXXXzo_ZPXXX-372-441.png --> return 372, 441
 * http://img.alicdn.com/tps/i1/TB1NZxRLFXXXXbwXFXXzo_ZPXXX-372-441.png --> return 372, 441
 * http://img07.taobaocdn.com/tfscom/T10DjXXn4oXXbSV1s__105829.jpg_100x100.jpg --> return 100, 100
 * http://img07.taobaocdn.com/tfscom/T10DjXXn4oXXbSV1s__105829.jpg_100x100q90.jpg --> return 100, 100
 * http://img07.taobaocdn.com/tfscom/T10DjXXn4oXXbSV1s__105829.jpg_100x100q90.jpg_.webp --> return 100, 100
 * http://img03.taobaocdn.com/tps/i3/T1JYROXuRhXXajR_DD-1680-446.jpg_q50.jpg --> return 1680, 446
 * </pre>
 * @param imageUrl image url
 * @return width and height pair parsed from url
 */
public static Pair<Integer, Integer> getImageSize(String imageUrl) {
    if (TextUtils.isEmpty(imageUrl))
        return null;

    try {
        Matcher matcher = REGEX_1.matcher(imageUrl);
        String widthStr;
        String heightStr;
        if (matcher.find()) {
            if (matcher.groupCount() >= 2) {
                widthStr = matcher.group(1);
                heightStr = matcher.group(2);
                if (widthStr.length() < 5 && heightStr.length() < 5) {
                    int urlWidth = Integer.parseInt(widthStr);
                    int urlHeight = Integer.parseInt(heightStr);

                    return new Pair<>(urlWidth, urlHeight);
                }
            }
        } else {
            matcher = REGEX_2.matcher(imageUrl);
            if (matcher.find()) {
                if (matcher.groupCount() >= 2) {
                    widthStr = matcher.group(1);
                    heightStr = matcher.group(2);
                    if (widthStr.length() < 5 && heightStr.length() < 5) {
                        int urlWidth = Integer.parseInt(widthStr);
                        int urlHeight = Integer.parseInt(heightStr);

                        return new Pair<>(urlWidth, urlHeight);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:org.apache.hadoop.hbase.regionserver.StoreFileInfo.java

public static boolean isHFile(final String fileName) {
    Matcher m = HFILE_NAME_PATTERN.matcher(fileName);
    return m.matches() && m.groupCount() > 0;
}

From source file:com.tmall.wireless.tangram.util.Utils.java

/**
 * <pre>/*from   w  w w .j  a  va 2  s . co  m*/
 * parse image ratio from imageurl with regex as follows:
 * (\d+)-(\d+)(_?q\d+)?(\.[jpg|png|gif])
 * (\d+)x(\d+)(_?q\d+)?(\.[jpg|png|gif])
 *
 * samples urls:
 * http://img.alicdn.com/tps/i1/TB1x623LVXXXXXZXFXXzo_ZPXXX-372-441.png --> return 372/441
 * http://img.alicdn.com/tps/i1/TB1P9AdLVXXXXa_XXXXzo_ZPXXX-372-441.png --> return 372/441
 * http://img.alicdn.com/tps/i1/TB1NZxRLFXXXXbwXFXXzo_ZPXXX-372-441.png --> return 372/441
 * http://img07.taobaocdn.com/tfscom/T10DjXXn4oXXbSV1s__105829.jpg_100x100.jpg --> return 100/100
 * http://img07.taobaocdn.com/tfscom/T10DjXXn4oXXbSV1s__105829.jpg_100x100q90.jpg --> return 100/100
 * http://img07.taobaocdn.com/tfscom/T10DjXXn4oXXbSV1s__105829.jpg_100x100q90.jpg_.webp --> return 100/100
 * http://img03.taobaocdn.com/tps/i3/T1JYROXuRhXXajR_DD-1680-446.jpg_q50.jpg --> return 1680/446
 * </pre>
 * @param imageUrl image url
 * @return ratio of with to height parsed from url
 */
public static float getImageRatio(String imageUrl) {
    if (TextUtils.isEmpty(imageUrl))
        return Float.NaN;

    try {
        Matcher matcher = REGEX_1.matcher(imageUrl);
        String widthStr;
        String heightStr;
        if (matcher.find()) {
            if (matcher.groupCount() >= 2) {
                widthStr = matcher.group(1);
                heightStr = matcher.group(2);
                if (widthStr.length() < 5 && heightStr.length() < 5) {
                    int urlWidth = Integer.parseInt(widthStr);
                    int urlHeight = Integer.parseInt(heightStr);

                    if (urlWidth == 0 || urlHeight == 0) {
                        return 1;
                    }
                    return (float) urlWidth / urlHeight;
                }
            }
        } else {
            matcher = REGEX_2.matcher(imageUrl);
            if (matcher.find()) {
                if (matcher.groupCount() >= 2) {
                    widthStr = matcher.group(1);
                    heightStr = matcher.group(2);
                    if (widthStr.length() < 5 && heightStr.length() < 5) {
                        int urlWidth = Integer.parseInt(widthStr);
                        int urlHeight = Integer.parseInt(heightStr);

                        if (urlWidth == 0 || urlHeight == 0) {
                            return 1;
                        }
                        return (float) urlWidth / urlHeight;
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return Float.NaN;
}

From source file:org.apache.cassandra.db.lifecycle.LogFile.java

static LogFile make(String fileName, List<File> logReplicas) {
    Matcher matcher = LogFile.FILE_REGEX.matcher(fileName);
    boolean matched = matcher.matches();
    assert matched && matcher.groupCount() == 3;

    // For now we don't need this but it is there in case we need to change
    // file format later on, the version is the sstable version as defined in BigFormat
    //String version = matcher.group(1);

    OperationType operationType = OperationType.fromFileName(matcher.group(2));
    UUID id = UUID.fromString(matcher.group(3));

    return new LogFile(operationType, id, logReplicas);
}

From source file:org.codehaus.groovy.grails.commons.GrailsResourceUtils.java

/**
 * Get the path relative to an artefact folder under grails-app i.e:
 *
 * Input: /usr/joe/project/grails-app/conf/BootStrap.groovy
 * Output: BootStrap.groovy/*from  ww  w. j a  va 2s  .c  o  m*/
 *
 * Input: /usr/joe/project/grails-app/domain/com/mystartup/Book.groovy
 * Output: com/mystartup/Book.groovy
 *
 * @param path The path to evaluate
 * @return The path relative to the root folder grails-app
 */
public static String getPathFromRoot(String path) {
    for (Pattern COMPILER_ROOT_PATTERN : COMPILER_ROOT_PATTERNS) {
        Matcher m = COMPILER_ROOT_PATTERN.matcher(path);
        if (m.find()) {
            return m.group(m.groupCount() - 1);
        }
    }
    return null;
}

From source file:com.feilong.core.util.RegexUtil.java

/**
 * ??????.// w ww .j a va2  s.  c o  m
 * 
 * <p>
 * ? m?? s  g,? m.group(g)  s.substring(m.start(g), m.end(g)).<br>
 * ? 1?.0?,? m.group(0) m.group().
 * </p>
 * 
 * <h3>:</h3>
 * 
 * <blockquote>
 * 
 * <pre class="code">
 * String regexPattern = "(.*?)@(.*?)";
 * String email = "venusdrogon@163.com";
 * RegexUtil.group(regexPattern, email);
 * </pre>
 * 
 * <b>:</b>
 * 
 * <pre class="code">
 *    0 venusdrogon@163.com
 *    1 venusdrogon
 *    2 163.com
 * </pre>
 * 
 * </blockquote>
 * 
 * @param regexPattern
 *            ?,pls use {@link RegexPattern}
 * @param input
 *            The character sequence to be matched,support {@link String},{@link StringBuffer},{@link StringBuilder}... and so on
 * @return  <code>regexPattern</code> null, {@link NullPointerException}<br>
 *          <code>input</code> null, {@link NullPointerException}<br>
 *          ??, {@link java.util.Collections#emptyMap()}
 * @see #getMatcher(String, CharSequence)
 * @see Matcher#group(int)
 * @since 1.0.7
 */
public static Map<Integer, String> group(String regexPattern, CharSequence input) {
    Matcher matcher = getMatcher(regexPattern, input);
    if (!matcher.matches()) {
        LOGGER.trace("[not matches] ,\n\tregexPattern:[{}] \n\tinput:[{}]", regexPattern, input);
        return Collections.emptyMap();
    }
    int groupCount = matcher.groupCount();
    Map<Integer, String> map = newLinkedHashMap(groupCount + 1);
    for (int i = 0; i <= groupCount; ++i) {
        //?
        String groupValue = matcher.group(i); //map.put(0, matcher.group());// ? 1 ?.0?,? m.group(0)  m.group().
        LOGGER.trace("matcher group[{}],start-end:[{}-{}],groupValue:[{}]", i, matcher.start(i), matcher.end(i),
                groupValue);
        map.put(i, groupValue);//groupValue
    }

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("regexPattern:[{}],input:[{}],groupMap:{}", regexPattern, input, JsonUtil.format(map));
    }
    return map;
}

From source file:com.cloudant.tests.UnicodeTest.java

/**
 * Returns the charset of a plain-text entity.
 */// w  w  w.  jav a 2s.c om
private static Charset getPlainTextEntityCharset(HttpConnection connection) {
    // For plain text, use the charset that is mentioned in the response
    // header field 'Content-Type'.
    // See http://stackoverflow.com/questions/3216730/with-httpclient-is-there-a-way-to-get
    // -the-character-set-of-the-page-with-a-head

    String contentType = connection.getConnection().getContentType();
    if (contentType == null) {
        contentType = "text/plain";
    }

    //look for any charset information in the Content-Type
    String charsetName = null;
    Matcher m = Pattern.compile(".*;\\s*charset=(^;)+.*", Pattern.CASE_INSENSITIVE).matcher(contentType);
    if (m.matches() && m.groupCount() >= 1) {
        charsetName = m.group(1);
    }

    Charset charset;
    if (charsetName == null) {
        // In the HTTP protocol, the default charset is ISO-8859-1.
        // But not for the Cloudant server:
        // - When we retrieve a document without specifying an 'Accept' header,
        //   it replies with a UTF-8 encoded JSON string and a header
        //   "Content-Type: text/plain;charset=utf-8".
        // - When we do the same thing with a "Accept: application/json" header,
        //   it replies with the same UTF-8 encoded JSON string and a header
        //   "Content-Type: application/json". So here the UTF-8 encoding must
        //   be implicitly understood.
        if ("application/json".equalsIgnoreCase(contentType)) {
            charset = Charset.forName("UTF-8");
        } else {
            charset = Charset.forName("ISO-8859-1");
        }
    } else {
        charset = Charset.forName(charsetName);
    }
    return charset;
}

From source file:cz.cas.lib.proarc.common.process.GenericExternalProcess.java

/**
 * Interpolate parameter values. The replace pattern is {@code ${name}}.
 * @param s a string to search for placeholders
 * @return the resolved string//from w w w.j a va2 s  .  c  o  m
 * @see #addParameter(java.lang.String, java.lang.String)
 */
static String interpolateParameters(String s, Map<String, String> parameters) {
    if (s == null || s.length() < 4 || parameters.isEmpty()) { // minimal replaceable value ${x}
        return s;
    }
    // finds ${name} patterns
    Matcher m = REPLACE_PARAM_PATTERN.matcher(s);
    StringBuffer sb = null;
    while (m.find()) {
        if (m.groupCount() == 1) {
            String param = m.group(1);
            String replacement = parameters.get(param);
            if (replacement != null) {
                sb = sb != null ? sb : new StringBuffer();
                m.appendReplacement(sb, Matcher.quoteReplacement(replacement));
            }
        }
    }
    if (sb == null) {
        return s;
    }
    m.appendTail(sb);
    return sb.toString();
}