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 String getNodeValue(NodeList nodeList)
        throws TransformerFactoryConfigurationError, TransformerException {

    final Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.setURIResolver(null);/*from  ww w .  j  a va2s  . co m*/

    final StringBuilder buf = new StringBuilder();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final StringWriter sw = new StringWriter();
        serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw));
        String xml = sw.toString();
        final Matcher matcher = HEADER_PATTERN.matcher(xml);
        if (matcher.matches()) {
            xml = matcher.group(1);
        }
        buf.append(xml);
        buf.append("\n");
    }

    return buf.toString();
}

From source file:Main.java

/**
 * Extracts a desired string given a string and a pattern using regex
 * //from   ww  w.jav  a 2  s .co  m
 * @param response the string to extract from
 * @param pattern the regex pattern used to extract 
 * 
 * @return desired extracted string
 */
@SuppressWarnings("deprecation")
public static String extract(String response, Pattern pattern) {
    String extraction = null;
    Matcher matcher = pattern.matcher(response);
    if (matcher.find() && matcher.groupCount() >= 1) {
        extraction = URLDecoder.decode(matcher.group(1));
    }
    return extraction;
}

From source file:Main.java

private static String urlEncode(String str, String charset) throws UnsupportedEncodingException {
    Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
    Matcher m = p.matcher(str);
    StringBuffer b = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(b, URLEncoder.encode(m.group(0), charset));
    }/*from   www. j  a  va  2  s  .  c  o m*/
    m.appendTail(b);
    return b.toString();
}

From source file:Main.java

/**
 * Extract default values of variables defined in the given string.
 * Default values are used to populate the variableDefs map.  Variables
 * already defined in this map will NOT be modified.
 *
 * @param string string to extract default values from.
 * @param variableDefs map which default values will be added to.
 *///w ww .  j av a 2s  . c o m
public static void extractVariableDefaultsFromString(String string, Map<String, String> variableDefs) {
    Matcher variableMatcher = variablePattern.matcher(string);
    while (variableMatcher.find()) {
        String varString = variableMatcher.group(1);

        int eqIdx = varString.indexOf("=");

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

            if (!variableDefs.containsKey(varName))
                variableDefs.put(varName, varString.substring(eqIdx + 1, varString.length()));
        }
    }
}

From source file:com.streamsets.pipeline.lib.jdbc.parser.sql.RawTypeHandler.java

public static byte[] parseRaw(String column, String value, int columnType) throws StageException {
    if (value == null) {
        return null;
    }/*from   w  w  w . j a v a  2  s .c  o  m*/
    Matcher m = HEX_TO_RAW_PATTERN.matcher(value);
    if (m.find()) {
        try {
            return Hex.decodeHex(m.group(1).toCharArray());
        } catch (DecoderException e) {
            throw new StageException(JDBC_204, m.group(1));
        }
    }
    throw new UnsupportedFieldTypeException(column, value, columnType);
}

From source file:Main.java

public static List<String> matchingToList(String regex, String input, int group) {

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    List<String> groups = new ArrayList<String>();
    while (matcher.find()) {
        groups.add(matcher.group(group));
    }/* w  w w . j  av  a  2 s  . c  o  m*/
    return groups;
}

From source file:Main.java

@Nullable
static String extractClientSecretFromHtml(@NonNull String html) {
    // quotes around attribute values are optional in HTML5: http://stackoverflow.com/q/6495310/504611
    Pattern clientSecretPattern = Pattern.compile(
            "^.*<meta[ ]+name=['\"]?env-clientSecret['\"]?[ ]+content=['\"]?([^'\"]+)['\"]?.*$",
            Pattern.DOTALL);//from   w w  w  . ja v  a2  s  .  co  m
    Matcher matcher = clientSecretPattern.matcher(html);
    String clientSecret = null;
    if (matcher.matches()) {
        clientSecret = matcher.group(1);
    }
    return clientSecret;
}

From source file:Main.java

public static String constructNextPageBookSearchUrl(String currentBookSearchUrl, int currentPage) {
    final Matcher matcher = NEXT_PAGE_URL_PATTERN.matcher(currentBookSearchUrl);
    if (matcher.find()) {
        currentPage = Integer.parseInt(matcher.group(1));
        currentPage++;/* ww w  .java2 s . c  om*/

        String cleanedBookSearchUrl = currentBookSearchUrl.replaceAll("(\\d+)$", "");
        return cleanedBookSearchUrl + currentPage;
    } else {
        currentPage++;
        return currentBookSearchUrl + "/page/" + currentPage;
    }
}

From source file:io.wcm.tooling.netbeans.sightly.completion.classLookup.ParsedStatement.java

/**
 * @param matcher// ww  w . j  a v  a 2s  . c om
 * @return Parsed statement
 */
public static ParsedStatement fromMatcher(Matcher matcher) {
    if (matcher.groupCount() >= 5) {
        String command = matcher.group(1);
        String variable = matcher.group(3);
        String value = matcher.group(5);
        if (StringUtils.isNotBlank(command) && StringUtils.isNotBlank(variable)
                && StringUtils.isNotBlank(value)) {
            return new ParsedStatement(command, variable, value);
        }
    }
    return null;
}

From source file:Main.java

public static Rectangle parseRectangle(String s) {
    if (rectanglePattern == null)
        rectanglePattern = Pattern.compile("\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*");

    Matcher m = rectanglePattern.matcher(s);

    if (m.matches()) {
        return new Rectangle(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)),
                Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4)));
    }//from   ww w.ja  v a  2s . c  om
    return null;
}