本文整理匯總了Java中java.util.regex.Pattern.split方法的典型用法代碼示例。如果您正苦於以下問題:Java Pattern.split方法的具體用法?Java Pattern.split怎麽用?Java Pattern.split使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.regex.Pattern
的用法示例。
在下文中一共展示了Pattern.split方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: get2InString
import java.util.regex.Pattern; //導入方法依賴的package包/類
/**
* @param style
* 類型
* @return 用逗號,或者分號截取字符串前兩個(這個方法用於類型的字符串截取)
*/
public static String get2InString(String style) {
Pattern pattern = Pattern.compile("[,;]");
String[] actors = pattern.split(style);
StringBuffer buffer = new StringBuffer();
if (actors.length <= 1) {
buffer.append(actors[0]);
} else if (actors.length == 2) {
buffer.append(actors[0]);
buffer.append(",");
buffer.append(actors[1]);
} else if (actors.length >= 3) {
buffer.append(actors[0]);
buffer.append(",");
buffer.append(actors[1]);
buffer.append(",");
buffer.append(actors[2]);
}
return buffer.toString();
}
示例2: process
import java.util.regex.Pattern; //導入方法依賴的package包/類
private ArrayList<String> process(String args[])
{
ArrayList<String> others = new ArrayList<String>();
final Pattern p = Pattern.compile("=");
for (int i = 0, cnt = args.length; i < cnt; i++) {
final String arg = args[i];
final String[] parts = p.split(arg, 2);
if (parts[0].startsWith("-"))
parts[0] = parts[0].substring(1);
// ArgumentsParser Parameter
if (PARAMS.contains(parts[0].toLowerCase())) {
this.params.put(parts[0].toLowerCase(), parts[1]);
}
else
others.add(arg);
} // FOR
return others;
}
示例3: parse
import java.util.regex.Pattern; //導入方法依賴的package包/類
/**
* Parse a description of partition ids and populate a PartitionSet.
* This supports comma-separated lists (e.g., "1,2,3,4") and
* ranges (e.g., "1-4"). You can also mix and match them together
* (e.g., "1,2-3,4").
* @param partitionList
* @return
*/
public static PartitionSet parse(String partitionList) {
Pattern HYPHEN_SPLIT = Pattern.compile(Pattern.quote("-"));
// Partition Ranges
PartitionSet partitions = new PartitionSet();
for (String p : StringUtil.splitList(partitionList)) {
int start = -1;
int stop = -1;
String range[] = HYPHEN_SPLIT.split(p);
if (range.length == 2) {
start = Integer.parseInt(range[0].trim());
stop = Integer.parseInt(range[1].trim());
} else {
start = Integer.parseInt(p.trim());
stop = start;
}
for (int partition = start; partition < stop + 1; partition++) {
partitions.add(partition);
} // FOR
} // FOR
return (partitions);
}
示例4: split
import java.util.regex.Pattern; //導入方法依賴的package包/類
static String[] split(final String s, final Pattern delimRegex1, final Pattern delimRegex2) {
String[] parts = delimRegex1.split(s);
if (parts.length == 1 && s.equals(parts[0])) {
//delimiter was not contained in string, try second delimiter
parts = delimRegex2.split(s);
if (parts.length == 1 && s.equals(parts[0])) {
//delimiter was not contained in string, we're done
return parts;
}
}
if (!s.startsWith(parts[0])) {
//must be delimiter at the start of the string, meaning we have an empty string at the start
final String[] newParts = new String[parts.length + 1];
System.arraycopy(parts, 0, newParts, 1, parts.length);
newParts[0] = "";
parts = newParts;
}
if (!s.endsWith(parts[parts.length - 1])) {
//must be delimiter at the end of the string, meaning we have an empty string at the end
parts = Arrays.copyOf(parts, parts.length + 1);
parts[parts.length - 1] = "";
}
return parts;
}
示例5: queryToMap
import java.util.regex.Pattern; //導入方法依賴的package包/類
public static Map<String, String[]> queryToMap(String query, Pattern andPattern, Pattern equalPattern) {
Map<String, String[]> parameters = new HashMap<String, String[]>();
for (String part : andPattern.split(query)) {
String[] pair = equalPattern.split(part, 2);
try {
String key = URLDecoder.decode(pair[0], "UTF-8");
String value = pair.length > 1 ? URLDecoder.decode(pair[1], "UTF-8") : "";
String[] list = parameters.get(key);
if (list != null) {
list = GenericUtils.copyOf(list, list.length + 1);
list[list.length - 1] = value;
} else {
list = new String[]{value};
}
parameters.put(key, list);
} catch (UnsupportedEncodingException e) {
Engine.logEngine.error("Unable to parse '" + part + "' as parameter", e);
}
}
return parameters;
}
示例6: testClassesPattern
import java.util.regex.Pattern; //導入方法依賴的package包/類
public void testClassesPattern() {
Pattern p = CssClassesVisitor.CLASSES_PATTERN;
CharSequence input = " one two ";
String[] parts = p.split(input);
assertEquals(3, parts.length);
assertTrue(parts[0].trim().isEmpty());
assertEquals("one", parts[1]);
assertEquals("two", parts[2]);
}
示例7: testHanPattern
import java.util.regex.Pattern; //導入方法依賴的package包/類
public void testHanPattern() {
Pattern hanP = Pattern.compile("([\\u4E00-\\u9FD5]+)");
String sentence = "abc我是中國人bc你好 workd";
Matcher matcher = hanP.matcher(sentence);
System.out.println(matcher.matches());
for (String s : hanP.split(sentence)) {
System.out.print(s + " / ");
}
}
示例8: tokenize
import java.util.regex.Pattern; //導入方法依賴的package包/類
public static List<String> tokenize(String line) {
Pattern pattern = Pattern.compile("\\W+");
String[] split = pattern.split(line.toLowerCase());
return Arrays.stream(split)
.map(String::trim)
.filter(s -> s.length() > 2)
.collect(Collectors.toList());
}
示例9: tokenizeFilter
import java.util.regex.Pattern; //導入方法依賴的package包/類
public static List<String> tokenizeFilter(String line) {
Pattern pattern = Pattern.compile("\\W+");
String[] split = pattern.split(line.toLowerCase());
return Arrays.stream(split)
.map(String::trim)
.filter(s -> s.length() > 2)
.filter(s -> !isStopword(s))
.collect(Collectors.toList());
}
示例10: loadFromArgs
import java.util.regex.Pattern; //導入方法依賴的package包/類
public void loadFromArgs(String args[]) {
if (debug.val) LOG.debug("Loading from commandline input arguments");
final Pattern split_p = Pattern.compile("=");
final Map<String, String> argsMap = new ListOrderedMap<String, String>();
for (int i = 0, cnt = args.length; i < cnt; i++) {
final String arg = args[i];
final String[] parts = split_p.split(arg, 2);
if (parts.length == 1) {
LOG.warn("Unexpected argument format '" + arg + "'");
continue;
}
String k = parts[0].toLowerCase();
String v = parts[1];
if (k.startsWith("-")) k = k.substring(1);
// 'hstore.tag' is special argument that we use in killstragglers.py
if (k.equalsIgnoreCase("tag")) {
continue;
// This command is undefined from the commandline
} else if (v.startsWith("${")) {
continue;
// Or this parameter is commented out in Eclipse
} else if (k.startsWith("#")) {
continue;
}
// We want it!
argsMap.put(k, v);
} // FOR
this.loadFromArgs(argsMap);
}
示例11: getProcessInfo
import java.util.regex.Pattern; //導入方法依賴的package包/類
public static Map<String, String> getProcessInfo(String pid, String... args) throws Exception {
if (args == null || args.length == 0) {
throw new IllegalArgumentException("args cannot be empty.");
}
StringBuilder sb = new StringBuilder();
sb.append("ps h -o ");
for (String arg : args) {
sb.append(arg).append(",");
}
sb.setLength(sb.length() - 1);
sb.append(" -p ").append(pid);
String rs = RuntimeHelper.exec(sb.toString());
if (rs == null || rs.length() == 0) {
return null;
}
Pattern p = Pattern.compile("\\s+");
String[] ss = p.split(rs.trim(), args.length);
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < ss.length; i++) {
map.put(args[i], ss[i]);
}
return map;
}
示例12: parsePoints
import java.util.regex.Pattern; //導入方法依賴的package包/類
private static List<Location> parsePoints(String points) {
Pattern patt = Pattern.compile("[ ,\n\r\t]+");
String[] toks = patt.split(points);
Location[] ret = new Location[toks.length / 2];
for (int i = 0; i < ret.length; i++) {
int x = Integer.parseInt(toks[2 * i]);
int y = Integer.parseInt(toks[2 * i + 1]);
ret[i] = Location.create(x, y);
}
return UnmodifiableList.create(ret);
}
示例13: add
import java.util.regex.Pattern; //導入方法依賴的package包/類
private static void add(String name, String soid, int type, String sfield,
String a, String b, String x, String y, String n, int h,
Pattern nameSplitPattern) {
BigInteger p = bi(sfield);
ECField field;
if ((type == P) || (type == PD)) {
field = new ECFieldFp(p);
} else if ((type == B) || (type == BD)) {
field = new ECFieldF2m(p.bitLength() - 1, p);
} else {
throw new RuntimeException("Invalid type: " + type);
}
EllipticCurve curve = new EllipticCurve(field, bi(a), bi(b));
ECPoint g = new ECPoint(bi(x), bi(y));
NamedCurve params = new NamedCurve(name, soid, curve, g, bi(n), h);
if (oidMap.put(soid, params) != null) {
throw new RuntimeException("Duplication oid: " + soid);
}
String[] commonNames = nameSplitPattern.split(name);
for (String commonName : commonNames) {
if (nameMap.put(commonName.trim(), params) != null) {
throw new RuntimeException("Duplication name: " + commonName);
}
}
int len = field.getFieldSize();
if ((type == PD) || (type == BD) || (lengthMap.get(len) == null)) {
// add entry if none present for this field size or if
// the curve is marked as a default curve.
lengthMap.put(len, params);
}
}
示例14: split
import java.util.regex.Pattern; //導入方法依賴的package包/類
public static List<String[]> split(String message, Pattern DELIMITER){
List<String[]> list = new ArrayList<String[]>();
String[] lines = message.split("\n");
for (String line : lines) {
String[] splits = DELIMITER.split(line.trim());
list.add(splits);
}
return list;
}
示例15: splitQuery
import java.util.regex.Pattern; //導入方法依賴的package包/類
private String[] splitQuery(String query) {
Pattern delimiterPattern = Pattern.compile("((?<= and )|(?= and )|(?<= or )|(?= or ))", Pattern.CASE_INSENSITIVE);
String[] parts = delimiterPattern.split(query);
for (int i = 0; i < parts.length; i++) {
parts[i] = parts[i].trim();
}
return parts;
}