Example usage for java.util.regex Pattern split

List of usage examples for java.util.regex Pattern split

Introduction

In this page you can find the example usage for java.util.regex Pattern split.

Prototype

public String[] split(CharSequence input, int limit) 

Source Link

Document

Splits the given input sequence around matches of this pattern.

Usage

From source file:util.StringList.java

public static StringList split(String s, Pattern regexp) {
    return new StringList(regexp.split(s, 0));
}

From source file:Main.java

/**
 * Create a URL from an API call (or "action string")
 *///from   w w w  .  jav  a 2  s. com
public static String getPageURL(String call) {
    Log.i("Jarvis", String.format("Call: %s", call));

    // Call's parts
    String function = "";
    String action = "";
    String data = "";

    // Final URL
    String url = "";
    Boolean isInternal = true;

    // This is weird left-over support for external URLs
    // Check if this is not a Jarvis URL
    if (call.length() >= 4 && call.substring(0, 4).equals("http")) {
        Integer length = API_ROOT.length();
        if (call.substring(0, length).equals(API_ROOT)) {
            call = call.substring(length + 1);
            call = call.replace("/", " ");
        } else {
            url = call;
            isInternal = false;
        }
    }

    if (isInternal) {
        // Encode URI data
        Pattern pattern = Pattern.compile(" ");
        String[] parts = pattern.split(call, 3);
        function = Uri.encode(parts[0]);
        if (parts.length > 1) {
            action = Uri.encode(parts[1]);
        }
        if (parts.length > 2) {
            data = Uri.encode(parts[2]);
        }

        // Final format
        url = String.format("%s/api/%s/%s/%s", API_ROOT, function, action, data);
    }

    Log.d("Jarvis", String.format("URL: %s", url));

    return url;
}

From source file:com.ibm.watson.catalyst.jumpqa.matcher.EntryPatterns.java

/** 
 * TODO: Method description//from w  w w. j  av  a  2s. c om
 * @param aString
 * @param p
 * @return
 */
private static List<String> splitText(final String aString, final Pattern p) {
    List<String> result = new ArrayList<String>();
    final String[] beforeAfter = p.split(aString, 2);
    result.add(beforeAfter[0].trim());

    final Matcher m = p.matcher(aString);
    if (m.find()) {
        result.add(m.group().trim());
        result.add(beforeAfter[1].trim());
    }
    return result;
}

From source file:mergedoc.core.FastStringUtils.java

/**
 * ?? regex ????/*from   w w w  .  j  a va2 s .  c o  m*/
 *
 * <p>????? JDK ? String#split(String,int)
 * ?????????? Pattern 
 * ?????? regex ???2 ????
 *
 * <pre>
 *     input.split(regex, limit);
 * </pre>
 *
 * ?<br>
 * regex ???????? Pattern ?
 * ????????????????
 *
 * @param input ??
 * @param regex ???
 * @param limit ??????
 * @return ????
 */
public static String[] split(String input, String regex, int limit) {
    Pattern pattern = PatternCache.getPattern(regex);
    return pattern.split(input, limit);
}

From source file:forge.util.FileSection.java

/**
 * Parses the.//  w  w  w. j a  va  2s  .c o  m
 *
 * @param lines the lines
 * @param kvSeparator the kv separator
 * @return the file section
 */
public static FileSection parse(final Iterable<String> lines, final String kvSeparator) {
    final FileSection result = new FileSection();
    final Pattern splitter = Pattern.compile(Pattern.quote(kvSeparator));
    for (final String dd : lines) {
        final String[] v = splitter.split(dd, 2);
        result.lines.put(v[0].trim(), v.length > 1 ? v[1].trim() : "");
    }

    return result;
}

From source file:forge.util.FileSection.java

public static Map<String, String> parseToMap(final String line, final String kvSeparator,
        final String pairSeparator) {
    Map<String, String> result = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    if (!StringUtils.isEmpty(line)) {
        final String[] pairs = line.split(Pattern.quote(pairSeparator));
        final Pattern splitter = Pattern.compile(Pattern.quote(kvSeparator));

        for (final String dd : pairs) {
            final String[] v = splitter.split(dd, 2);
            result.put(v[0].trim(), v.length > 1 ? v[1].trim() : "");
        }/*w  ww.  j  a v a 2 s  .  c om*/
    }
    return result;
}

From source file:android.syncml.pim.PropertyNode.java

public static PropertyNode decode(String encodedString) {
    PropertyNode propertyNode = new PropertyNode();
    String trimed = encodedString.trim();
    if (trimed.length() == 0) {
        return propertyNode;
    }/*from w w w.  j  a  va 2  s  .  c o  m*/
    String[] elems = trimed.split("],");

    for (String elem : elems) {
        int index = elem.indexOf('[');
        String name = elem.substring(0, index - 1);
        Pattern pattern = Pattern.compile("(?<!\\\\),");
        String[] values = pattern.split(elem.substring(index + 1), -1);
        if (name.equals("propName")) {
            propertyNode.propName = values[0];
        } else if (name.equals("propGroupSet")) {
            for (String value : values) {
                propertyNode.propGroupSet.add(value);
            }
        } else if (name.equals("paramMap")) {
            ContentValues paramMap = propertyNode.paramMap;
            Set<String> paramMap_TYPE = propertyNode.paramMap_TYPE;
            for (String value : values) {
                String[] tmp = value.split("=", 2);
                String mapKey = tmp[0];
                // \, -> ,
                // \\ -> \
                // In String#replaceAll(), "\\\\" means a single backslash.
                String mapValue = tmp[1].replaceAll("\\\\,", ",").replaceAll("\\\\\\\\", "\\\\");
                if (mapKey.equalsIgnoreCase("TYPE")) {
                    paramMap_TYPE.add(mapValue);
                } else {
                    paramMap.put(mapKey, mapValue);
                }
            }
        } else if (name.equals("propValue")) {
            StringBuilder builder = new StringBuilder();
            List<String> list = propertyNode.propValue_vector;
            int length = values.length;
            for (int i = 0; i < length; i++) {
                String normValue = values[i].replaceAll("\\\\,", ",").replaceAll("\\\\\\\\", "\\\\");
                list.add(normValue);
                builder.append(normValue);
                if (i < length - 1) {
                    builder.append(";");
                }
            }
            propertyNode.propValue = builder.toString();
        }
    }

    // At this time, QUOTED-PRINTABLE is already decoded to Java String.
    // We just need to decode BASE64 String to binary.
    String encoding = propertyNode.paramMap.getAsString("ENCODING");
    if (encoding != null && (encoding.equalsIgnoreCase("BASE64") || encoding.equalsIgnoreCase("B"))) {
        propertyNode.propValue_bytes = Base64.decodeBase64(propertyNode.propValue_vector.get(0).getBytes());
    }

    return propertyNode;
}

From source file:com.earthman.app.contactsave.syncml.PropertyNode.java

public static PropertyNode decode(String encodedString) {
    PropertyNode propertyNode = new PropertyNode();
    String trimed = encodedString.trim();
    if (trimed.length() == 0) {
        return propertyNode;
    }//  w  w  w. java 2 s  .c  om
    String[] elems = trimed.split("],");

    for (String elem : elems) {
        int index = elem.indexOf('[');
        String name = elem.substring(0, index - 1);
        Pattern pattern = Pattern.compile("(?<!\\\\),");
        String[] values = pattern.split(elem.substring(index + 1), -1);
        if (name.equals("propName")) {
            propertyNode.propName = values[0];
        } else if (name.equals("propGroupSet")) {
            for (String value : values) {
                propertyNode.propGroupSet.add(value);
            }
        } else if (name.equals("paramMap")) {
            com.earthman.app.contactsave.content.ContentValues paramMap = propertyNode.paramMap;
            Set<String> paramMap_TYPE = propertyNode.paramMap_TYPE;
            for (String value : values) {
                String[] tmp = value.split("=", 2);
                String mapKey = tmp[0];
                // \, -> ,
                // \\ -> \
                // In String#replaceAll(), "\\\\" means a single backslash.
                String mapValue = tmp[1].replaceAll("\\\\,", ",").replaceAll("\\\\\\\\", "\\\\");
                if (mapKey.equalsIgnoreCase("TYPE")) {
                    paramMap_TYPE.add(mapValue);
                } else {
                    paramMap.put(mapKey, mapValue);
                }
            }
        } else if (name.equals("propValue")) {
            StringBuilder builder = new StringBuilder();
            List<String> list = propertyNode.propValue_vector;
            int length = values.length;
            for (int i = 0; i < length; i++) {
                String normValue = values[i].replaceAll("\\\\,", ",").replaceAll("\\\\\\\\", "\\\\");
                list.add(normValue);
                builder.append(normValue);
                if (i < length - 1) {
                    builder.append(";");
                }
            }
            propertyNode.propValue = builder.toString();
        }
    }

    // At this time, QUOTED-PRINTABLE is already decoded to Java String.
    // We just need to decode BASE64 String to binary.
    String encoding = propertyNode.paramMap.getAsString("ENCODING");
    if (encoding != null && (encoding.equalsIgnoreCase("BASE64") || encoding.equalsIgnoreCase("B"))) {
        propertyNode.propValue_bytes = Base64.decodeBase64(propertyNode.propValue_vector.get(0).getBytes());
    }

    return propertyNode;
}

From source file:forge.util.FileUtil.java

public static List<Pair<String, String>> readNameUrlFile(String nameUrlFile) {
    Pattern lineSplitter = Pattern.compile(Pattern.quote(" "));
    Pattern replacer = Pattern.compile(Pattern.quote("%20"));

    List<Pair<String, String>> list = new ArrayList<Pair<String, String>>();

    for (String line : readFile(nameUrlFile)) {
        if (StringUtils.isBlank(line) || line.startsWith("#")) {
            continue;
        }/*  ww w.  j a v a2  s  . c  om*/

        String[] parts = lineSplitter.split(line, 2);
        if (2 == parts.length) {
            list.add(Pair.of(replacer.matcher(parts[0]).replaceAll(" "), parts[1]));
        } else {
            // figure out the filename from the URL
            Pattern pathSplitter = Pattern.compile(Pattern.quote("/"));
            String[] pathParts = pathSplitter.split(parts[0]);
            String last = pathParts[pathParts.length - 1];
            list.add(Pair.of(replacer.matcher(last).replaceAll(" "), parts[0]));
        }
    }

    return list;
}

From source file:forge.game.spellability.AbilityManaPart.java

/**
 * <p>// w  w  w  .  ja va 2  s .  c o m
 * applyManaReplacement.
 * </p>
 * @return a String
 */
public static String applyManaReplacement(final SpellAbility sa, final String original) {
    final HashMap<String, String> repMap = new HashMap<String, String>();
    final Player act = sa != null ? sa.getActivatingPlayer() : null;
    final String manaReplace = sa != null ? sa.getManaPart().getManaReplaceType() : "";
    if (manaReplace.isEmpty()) {
        if (act != null && act.getLandsPlayedThisTurn() > 0 && sa.hasParam("ReplaceIfLandPlayed")) {
            return sa.getParam("ReplaceIfLandPlayed");
        }
        return original;
    }
    if (manaReplace.startsWith("Any")) {
        // Replace any type and amount
        String replaced = manaReplace.split("->")[1];
        if (replaced.equals("Any")) {
            byte rs = MagicColor.GREEN;
            if (act != null) {
                rs = act.getController().chooseColor("Choose a color", sa, ColorSet.ALL_COLORS);
            }
            replaced = MagicColor.toShortString(rs);
        }
        return replaced;
    }
    final Pattern splitter = Pattern.compile("->");
    // Replace any type
    for (String part : manaReplace.split(" & ")) {
        final String[] v = splitter.split(part, 2);
        if (v[0].equals("Colorless")) {
            repMap.put("[0-9][0-9]?", v.length > 1 ? v[1].trim() : "");
        } else {
            repMap.put(v[0], v.length > 1 ? v[1].trim() : "");
        }
    }
    // Handle different replacement simultaneously
    Pattern pattern = Pattern.compile(StringUtils.join(repMap.keySet().iterator(), "|"));
    Matcher m = pattern.matcher(original);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        if (m.group().matches("[0-9][0-9]?")) {
            final String rep = StringUtils.repeat(repMap.get("[0-9][0-9]?") + " ", Integer.parseInt(m.group()))
                    .trim();
            m.appendReplacement(sb, rep);
        } else {
            m.appendReplacement(sb, repMap.get(m.group()));
        }
    }
    m.appendTail(sb);
    String replaced = sb.toString();
    while (replaced.contains("Any")) {
        byte rs = MagicColor.GREEN;
        if (act != null) {
            rs = act.getController().chooseColor("Choose a color", sa, ColorSet.ALL_COLORS);
        }
        replaced = replaced.replaceFirst("Any", MagicColor.toShortString(rs));
    }
    return replaced;
}