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() 

Source Link

Document

Returns the input subsequence matched by the previous match.

Usage

From source file:Main.java

public static String[] parseStringList(String list) {
    //        final Pattern patWs = Pattern.compile("\\s+");
    final Matcher matchWs = Pattern.compile("[^\\s]+").matcher("");
    matchWs.reset(list);/*from w w w .j a  va 2s . c o  m*/

    LinkedList<String> matchList = new LinkedList<String>();
    while (matchWs.find()) {
        matchList.add(matchWs.group());
    }

    String[] retArr = new String[matchList.size()];
    return (String[]) matchList.toArray(retArr);
}

From source file:Main.java

public static String escape(String message) {
    if (message == null)
        return message;
    Matcher matcher = escapePattern.matcher(message);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        String replacement;/* w ww .j a v a 2 s.c o m*/
        if ("\"".equals(matcher.group()))
            replacement = "&quot;";
        else if ("&".equals(matcher.group()))
            replacement = "&amp;";
        else if ("'".equals(matcher.group()))
            replacement = "&apos;";
        else if ("<".equals(matcher.group()))
            replacement = "&lt;";
        else if (">".equals(matcher.group()))
            replacement = "&gt;";
        else
            throw new IllegalArgumentException();
        matcher.appendReplacement(sb, replacement);
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:io.lavagna.web.api.EndpointInfoController.java

private static Set<String> extractPathVariables(String pattern) {

    Set<String> identifierAndPathVariable = new HashSet<>();
    // match stuff like "/project/{projectShortName}" or /{projectShortName}/value
    Pattern p = Pattern.compile("(/[^/]+/\\{[^\\}]+\\})|(\\{[^\\}]+\\}/[^/]+/)");
    Matcher m = p.matcher(pattern);
    while (m.find()) {
        identifierAndPathVariable.add(m.group());
    }/*ww w.j a  v a 2s .  c om*/

    return identifierAndPathVariable;
}

From source file:net.gkovalechyn.minereset.Util.java

public static long timeStringToLong(String string) {
    Validate.notNull(string);//from  w w  w  . j  a  v a2s  . co  m
    long res = 0l;
    Matcher mat = Util.timePattern.matcher(string);

    while (mat.find()) {
        String a = mat.group();
        long l = Long.parseLong(a.substring(0, a.length() - 1));

        switch (a.charAt(a.length() - 1)) {
        case 's':
            l *= 1000L;
            break;
        case 'm': //minutes
            l *= 60000l;
            break;
        case 'h': //hours
            l *= 3600000l;
            break;
        case 'd': //days
            l *= 86400000l;
            break;
        case 'w': //weeks
            l *= 604800000l;
            break;
        case 'M': //months
            l *= 2419200000l;
            break;
        case 'y': //years
            l *= 29030400000l;
            break;
        }
        res += l;
    }
    return res;
}

From source file:com.vaadin.sass.internal.visitor.IfElseNodeHandler.java

private static String replaceStrings(String expression) {
    expression = expression.replaceAll("\"", "");
    Matcher m = pattern.matcher(expression);
    StringBuffer b = new StringBuffer();
    while (m.find()) {
        String group = m.group();
        m.appendReplacement(b, "'" + group + "'");
    }//w  w w  .  jav a 2 s  .c  o m
    m.appendTail(b);
    if (b.length() != 0) {
        return b.toString();
    }
    return expression;
}

From source file:Main.java

/**
 * entitize//from w  ww .j av a2 s  .c o m
 * 
 * @param source
 * @return
 */
public static String entitize(String source) {
    String result = null;

    if (source != null) {
        Matcher m = SPECIAL_CHARS.matcher(source);
        StringBuffer sb = new StringBuffer();

        while (m.find()) {
            m.appendReplacement(sb, ENTITIES.get(m.group()));
        }

        m.appendTail(sb);

        result = sb.toString();
    }

    return result;
}

From source file:Main.java

public static void destroy(Process process) {
    // stupid method for getting the pid, but it actually works
    Matcher matcher = PID_PATTERN.matcher(process.toString());
    matcher.find();/*from  w ww.j  a  v a2 s  .c  o  m*/
    int pid = Integer.parseInt(matcher.group());
    List<Integer> allRelatedPids = getAllRelatedPids(pid);
    for (Integer relatedPid : allRelatedPids) {
        destroyPid(relatedPid);
    }
}

From source file:io.spring.initializr.actuate.stat.ProjectRequestDocumentFactory.java

private static String extractIpv4(String candidate) {
    if (StringUtils.hasText(candidate)) {
        Matcher matcher = IP_PATTERN.matcher(candidate);
        if (matcher.find()) {
            return matcher.group();
        }//from  w w w  . j a v  a  2 s.  c o m
    }
    return null;
}

From source file:org.archive.crawler.processor.HashCrawlMapper.java

public static String mapString(String key, String reducePattern, long bucketCount) {

    if (reducePattern != null && reducePattern.length() > 0) {
        Matcher matcher = TextUtils.getMatcher(reducePattern, key);
        if (matcher.find()) {
            key = matcher.group();
        }/* w w  w.ja v a  2s  .com*/
        TextUtils.recycleMatcher(matcher);
    }
    long fp = FPGenerator.std64.fp(key);
    long bucket = fp % bucketCount;
    return Long.toString(bucket >= 0 ? bucket : -bucket);
}

From source file:com.processpuzzle.fundamental_types.domain.ParameterValue.java

public static ParameterValue parse(String definitionText)
        throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    ParameterDefinition definition = ParameterDefinition.parse(definitionText);

    Pattern valuePattern = Pattern.compile("=([^/]+)/{0,2}");
    Matcher valueFinder = valuePattern.matcher(definitionText);
    String valueString = null;/*  w  ww  . j a  v a 2 s .  c  o m*/
    if (valueFinder.find()) {
        int valueStringLength = valueFinder.group().contains("//") ? valueFinder.group().length() - 2
                : valueFinder.group().length();
        valueString = StringUtils.strip(valueFinder.group().substring(1, valueStringLength));
    }

    Object valueObject = null;
    if (definition.getType().equals(String.class))
        valueObject = new String(valueString);
    else if (definition.getType().equals(Integer.class))
        valueObject = new Integer(valueString);
    else if (definition.getType().equals(Long.class))
        valueObject = new Long(valueString);
    else if (definition.getType().equals(Double.class))
        valueObject = new Double(valueString);

    return new ParameterValue(definition, valueObject);
}