List of usage examples for java.util.regex Matcher groupCount
public int groupCount()
From source file:com.tmall.wireless.tangram3.util.Utils.java
public static float getImageRatio(String imageUrl) { if (TextUtils.isEmpty(imageUrl)) return Float.NaN; try {/*from ww w .j av a2 s. c o m*/ 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:com.tmall.wireless.tangram3.util.Utils.java
/** * <pre>/*from www.j a va 2 s.c o 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 Pair<Integer, Integer> getImageSize(String imageUrl) { if (TextUtils.isEmpty(imageUrl)) { return null; } if (imageSizeMap.get(imageUrl) != null) { return imageSizeMap.get(imageUrl); } 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); Pair<Integer, Integer> result = new Pair<>(urlWidth, urlHeight); imageSizeMap.put(imageUrl, result); return result; } } } 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); Pair<Integer, Integer> result = new Pair<>(urlWidth, urlHeight); imageSizeMap.put(imageUrl, result); return result; } } } } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:org.apache.metron.stellar.common.shell.cli.StellarShellOptionsValidator.java
/** * Zookeeper argument should be in the form [HOST|IP]:PORT. * * @param zMulti the zookeeper url fragment *///from w w w.ja v a 2s. co m private static void validateZookeeperOption(String zMulti) throws IllegalArgumentException { for (String z : Splitter.on(",").split(zMulti)) { Matcher matcher = validPortPattern.matcher(z); boolean hasPort = z.contains(":"); if (hasPort && !matcher.matches()) { throw new IllegalArgumentException(String.format("Zookeeper option must have valid port: %s", z)); } if (hasPort && matcher.groupCount() != 2) { throw new IllegalArgumentException( String.format("Zookeeper Option must be in the form of [HOST|IP]:PORT %s", z)); } String name = hasPort ? matcher.group(1) : z; Integer port = hasPort ? Integer.parseInt(matcher.group(2)) : null; if (!hostnameValidator.test(name) && !inetAddressValidator.isValid(name)) { throw new IllegalArgumentException( String.format("Zookeeper Option %s is not a valid host name or ip address %s", name, z)); } if (hasPort && (port == 0 || port > 65535)) { throw new IllegalArgumentException(String.format("Zookeeper Option %s port is not valid", z)); } } }
From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java
public static JSONObject parseArgs(String... args) { final Pattern[] opts = new Pattern[] { Pattern.compile("^--([\\w\\d\\-]++)"), Pattern.compile("^-([\\w\\d\\-]++)"), Pattern.compile("^--([\\w\\d\\-]++)=(.*)"), Pattern.compile("^-([\\w\\d\\-]++)=(.*)") }; final JSONObject result = new JSONObject(); final JSONArray resultArgs = openArray(result, "args"); final JSONArray resultValues = openArray(result, "values"); final JSONObject options = openObject(result, "options"); args: for (final String arg : args) { resultArgs.put(arg);//w w w .jav a2 s . com for (final Pattern opt : opts) { final Matcher matcher = opt.matcher(arg); if (matcher.matches()) { try { switch (matcher.groupCount()) { case 1: options.put(matcher.group(1), true); break; case 2: options.put(matcher.group(1), matcher.group(2)); break; } } catch (JSONException e) { throw new AssertionError(e); } continue args; } } resultValues.put(arg); } return result; }
From source file:org.apache.nifi.authorization.util.IdentityMappingUtil.java
/** * Checks the given identity against each provided mapping and performs the mapping using the first one that matches. * If none match then the identity is returned as is. * * @param identity the identity to map/*w w w. j a va2 s . co m*/ * @param mappings the mappings * @return the mapped identity, or the same identity if no mappings matched */ public static String mapIdentity(final String identity, List<IdentityMapping> mappings) { for (IdentityMapping mapping : mappings) { Matcher m = mapping.getPattern().matcher(identity); if (m.matches()) { final String pattern = mapping.getPattern().pattern(); final String replacementValue = escapeLiteralBackReferences(mapping.getReplacementValue(), m.groupCount()); return identity.replaceAll(pattern, replacementValue); } } return identity; }
From source file:br.com.blackhubos.commandteleporter.Framework.java
public static Location toLocation(final String serial) { final Pattern pattern = Pattern.compile( "^World\\s*\\[([a-zA-Z0-9_-]+)\\]\\s*X\\s*\\[([0-9-]+)\\]\\s*Y\\s*\\[([0-9-]+)\\]\\s*Z\\s*\\[([0-9-]+)\\](\\s*Yaw\\s*\\[([0-9-\\.]+)\\]\\s*Pitch\\s*\\[([0-9-\\.]+)\\])?"); final Matcher m = pattern.matcher(serial); if (m.matches()) { if ((m.groupCount() >= 5) && (m.group(5) != null) && (m.group(6) != null) && (m.group(7) != null)) { return new Location(Framework.getWorld(m.group(1)), Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4)), Float.parseFloat(m.group(6)), Float.parseFloat(m.group(7))); } else {/*from ww w .j a v a2 s. c o m*/ return new Location(Framework.getWorld(m.group(1)), Integer.parseInt(m.group(2)), Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4))); } } else { return null; } }
From source file:com.linkedin.drelephant.util.MemoryFormatUtils.java
/** * Convert a formatted string into a long value in bytes. * This method handles//from www .java 2 s . com * * @param formattedString The string to convert * @return The bytes value */ public static long stringToBytes(String formattedString) { if (formattedString == null) { return 0L; } Matcher matcher = REGEX_MATCHER.matcher(formattedString); if (!matcher.matches()) { throw new IllegalArgumentException("The formatted string [" + formattedString + "] does not match with the regex /" + REGEX_MATCHER.toString() + "/"); } if (matcher.groupCount() != 1 && matcher.groupCount() != 2) { throw new IllegalArgumentException(); } double numPart = Double.parseDouble(matcher.group(1)); if (numPart < 0) { throw new IllegalArgumentException( "The number part of the memory cannot be less than zero: [" + numPart + "]."); } String unitPart = matcher.groupCount() == 2 ? matcher.group(2).toUpperCase() : ""; if (!unitPart.endsWith("B")) { unitPart += "B"; } for (int i = 0; i < UNITS.length; i++) { if (unitPart.equals(UNITS[i].getName())) { return (long) (numPart * UNITS[i].getBytes()); } } throw new IllegalArgumentException("The formatted string [" + formattedString + "] 's unit part [" + unitPart + "] does not match any unit. The supported units are (case-insensitive, and also the 'B' is ignorable): [" + StringUtils.join(UNITS) + "]."); }
From source file:jp.go.nict.langrid.servicesupervisor.frontend.FrontEnd.java
private static String getSoapRpcencodedCallTree(InputStream body) throws IOException { Matcher m = soapct.matcher(StreamUtil.readAsString(body, CharsetUtil.newUTF8Decoder())); if (!m.find()) return ""; if (m.groupCount() != 3) return ""; return StringEscapeUtils.unescapeHtml(m.group(2)); }
From source file:org.apache.roller.weblogger.config.PingConfig.java
/** * Initialize the common ping targets from the configuration properties. If the current list of common ping targets * is empty, and the <code>PINGS_INITIAL_COMMON_TARGETS_PROP</code> property is present in the configuration then, * this method will use that value to initialize the common targets. This is called on each server startup. * <p/>//from ww w .ja va 2 s . c o m * Note: this is expected to be called during initialization with transaction demarcation being handled by the * caller. * * @see org.apache.roller.weblogger.ui.core.RollerContext#contextInitialized(javax.servlet.ServletContextEvent) */ public static void initializeCommonTargets() throws WebloggerException { String configuredVal = WebloggerConfig.getProperty(PINGS_INITIAL_COMMON_TARGETS_PROP); if (configuredVal == null || configuredVal.trim().length() == 0) { if (logger.isDebugEnabled()) { logger.debug("No (or empty) value of " + PINGS_INITIAL_COMMON_TARGETS_PROP + " present in the configuration. Skipping initialization of commmon targets."); } return; } PingTargetManager pingTargetMgr = WebloggerFactory.getWeblogger().getPingTargetManager(); if (!pingTargetMgr.getCommonPingTargets().isEmpty()) { if (logger.isDebugEnabled()) { logger.debug( "Some common ping targets are present in the database already. Skipping initialization."); } return; } String[] configuredTargets = configuredVal.trim().split(","); for (int i = 0; i < configuredTargets.length; i++) { // Trim space around the target spec String thisTarget = configuredTargets[i].trim(); // skip empty ones if (thisTarget.length() == 0) continue; // parse the ith target and store it Matcher m = NESTED_BRACE_PAIR.matcher(thisTarget); if (m.matches() && m.groupCount() == 2) { String name = m.group(1).trim(); String url = m.group(2).trim(); logger.info("Creating common ping target '" + name + "' from configuration properties."); PingTarget pingTarget = new PingTarget(null, name, url, null, false); pingTargetMgr.savePingTarget(pingTarget); } else { logger.error("Unable to parse configured initial ping target '" + thisTarget + "'. Skipping this target. Check your setting of the property " + PINGS_INITIAL_COMMON_TARGETS_PROP); } } }
From source file:org.apache.roller.weblogger.config.PingConfig.java
/** * Initialize known ping variants from the configuration. *//*w w w . ja v a2 s . c o m*/ public static void initializePingVariants() { String configuredVal = WebloggerConfig.getProperty(PINGS_VARIANT_OPTIONS_PROP); if (configuredVal == null || configuredVal.trim().length() == 0) { if (logger.isDebugEnabled()) { logger.debug("No (or empty) value of " + PINGS_VARIANT_OPTIONS_PROP + " present in the configuration. Skipping initialization of ping variants."); } return; } String[] variants = configuredVal.trim().split(","); for (int i = 0; i < variants.length; i++) { String thisVariant = variants[i].trim(); if (thisVariant.length() == 0) continue; Matcher m = NESTED_BRACE_PAIR.matcher(thisVariant); if (m.matches() && m.groupCount() == 2) { String url = m.group(1).trim(); String optionsList = m.group(2).trim(); Set variantOptions = new HashSet(); String[] options = optionsList.split(","); for (int j = 0; j < options.length; j++) { String option = options[j].trim().toLowerCase(); if (option.length() > 0) { variantOptions.add(option); } } if (!variantOptions.isEmpty()) { configuredVariants.put(url, variantOptions); } else { logger.warn("Ping variant entry for url '" + url + "' has an empty variant options list. Ignored."); } } else { logger.error("Unable to parse configured ping variant '" + thisVariant + "'. Skipping this variant. Check your setting of the property " + PINGS_VARIANT_OPTIONS_PROP); } } }