List of usage examples for java.util.regex Pattern split
public String[] split(CharSequence input)
From source file:Main.java
public static String[] versionSplitting(String version, String regexVersionDelimiters) { Pattern pattern = Pattern.compile(regexVersionDelimiters); return pattern.split(version); }
From source file:Main.java
/** * Split a string into a list based on a pattern. * /* w ww.ja v a 2 s .c o m*/ * @param s the string to split. * @param pattern the pattern where to cut the string. * @return the splitted string, empty list if <code>s</code> is "". */ static public List<String> split(String s, Pattern pattern) { return s.length() == 0 ? Collections.<String>emptyList() : Arrays.asList(pattern.split(s)); }
From source file:com.cloudera.oryx.rdf.computation.CovtypeIT.java
private static List<Example> readCovtypeExamples() throws IOException { List<Example> allExamples = Lists.newArrayList(); Pattern delimiter = Pattern.compile(","); File dataFile = new File(TEST_TEMP_INBOUND_DIR, "covtype.csv.gz"); for (CharSequence line : new FileLineIterable(dataFile)) { String[] tokens = delimiter.split(line); Feature[] features = new Feature[54]; for (int i = 0; i < 10; i++) { features[i] = NumericFeature.forValue(Float.parseFloat(tokens[i])); }//from www . j ava 2 s. c o m for (int i = 10; i < 54; i++) { features[i] = CategoricalFeature.forValue(Integer.parseInt(tokens[i])); } Example trainingExample = new Example(CategoricalFeature.forValue(Integer.parseInt(tokens[54]) - 1), features); allExamples.add(trainingExample); } return allExamples; }
From source file:org.apache.hadoop.hive.ql.exec.mr.Throttle.java
/** * Fetch http://tracker.om:/gc.jsp?threshold=period. *///from w w w . j av a 2s.c o m public static void checkJobTracker(JobConf conf, Log LOG) { try { byte[] buffer = new byte[1024]; int threshold = conf.getInt("mapred.throttle.threshold.percent", DEFAULT_MEMORY_GC_PERCENT); int retry = conf.getInt("mapred.throttle.retry.period", DEFAULT_RETRY_PERIOD); // If the threshold is 100 percent, then there is no throttling if (threshold == 100) { return; } // This is the Job Tracker URL String tracker = JobTrackerURLResolver.getURL(conf) + "/gc.jsp?threshold=" + threshold; while (true) { // read in the first 1K characters from the URL URL url = new URL(tracker); LOG.debug("Throttle: URL " + tracker); InputStream in = null; try { in = url.openStream(); in.read(buffer); in.close(); in = null; } finally { IOUtils.closeStream(in); } String fetchString = new String(buffer); // fetch the xml tag <dogc>xxx</dogc> Pattern dowait = Pattern.compile("<dogc>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); String[] results = dowait.split(fetchString); if (results.length != 2) { throw new IOException( "Throttle: Unable to parse response of URL " + url + ". Get retuned " + fetchString); } dowait = Pattern.compile("</dogc>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); results = dowait.split(results[1]); if (results.length < 1) { throw new IOException( "Throttle: Unable to parse response of URL " + url + ". Get retuned " + fetchString); } // if the jobtracker signalled that the threshold is not exceeded, // then we return immediately. if (results[0].trim().compareToIgnoreCase("false") == 0) { return; } // The JobTracker has exceeded its threshold and is doing a GC. // The client has to wait and retry. LOG.warn("Job is being throttled because of resource crunch on the " + "JobTracker. Will retry in " + retry + " seconds.."); Thread.sleep(retry * 1000L); } } catch (Exception e) { LOG.warn("Job is not being throttled. " + e); } }
From source file:gov.nih.nci.cabig.ctms.web.taglibs.Functions.java
private static String newlinesToBr(String text, String brTag) { if (text == null) return null; Pattern newlinePattern = StringTools.NEWLINE_PATTERN; if (newlinePattern.matcher(text).matches()) { return brTag + '\n'; } else {/*from www. j a v a 2 s .com*/ String[] lines = newlinePattern.split(text); if (lines.length == 1) { return text; } else { boolean endsWithNl = newlinePattern.matcher(text).find(text.length() - 1); StringBuffer reassembled = new StringBuffer(text.length() + lines.length * brTag.length()); for (int i = 0; i < lines.length; i++) { reassembled.append(lines[i]); if (endsWithNl || i < lines.length - 1) { reassembled.append(brTag).append('\n'); } } return reassembled.toString(); } } }
From source file:com.cburch.draw.shapes.SvgReader.java
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); }// ww w .j av a2s . co m return UnmodifiableList.decorate(Arrays.asList(ret)); }
From source file:org.oscarehr.decisionSupport.model.conditionValue.DSValue.java
public static List<DSValue> createDSValues(String values) { String[] dsValuesStr = new String[0]; boolean doHyphenSearch = true; Pattern stringQuotePattern = Pattern.compile("'.+?'"); if (stringQuotePattern.matcher(values).find()) { //if has a pair of quotes with something in it - treat as a list of strings Pattern stringSeparatorPattern = Pattern.compile("'[\\s]*,"); String[] separatedValues = stringSeparatorPattern.split(values); // [ ',' ] is absolutely illegal in a quoted string ArrayList<String> dsValueStrArray = new ArrayList<String>(); for (String separatedValue : separatedValues) { if (!separatedValue.trim().endsWith("'")) { separatedValue = separatedValue.trim() + "'"; }// ww w . j a v a 2s .com dsValueStrArray.add(separatedValue); _log.debug("Separated Value: " + separatedValue); } dsValuesStr = dsValueStrArray.toArray(dsValuesStr); doHyphenSearch = false; } else { dsValuesStr = StringUtils.split(values, ","); } ArrayList<DSValue> dsValues = new ArrayList<DSValue>(); for (String dsValueStr : dsValuesStr) { int hyphenIndex = -1; if (doHyphenSearch) hyphenIndex = dsValueStr.indexOf("-"); //if specified range, i.e. "age: 1 y - 5 y" if (hyphenIndex != -1) { int colonIndex = dsValueStr.indexOf(":"); String type = ""; if (colonIndex != -1) type = dsValueStr.substring(0, colonIndex + 1); String value1 = dsValueStr.substring(colonIndex + 1, hyphenIndex); String value2 = dsValueStr.substring(hyphenIndex + 1); String dsValueStr1 = type + ">=" + value1; String dsValueStr2 = type + "<=" + value2; dsValues.add(DSValue.createDSValue(dsValueStr1)); dsValues.add(DSValue.createDSValue(dsValueStr2)); } else { dsValues.add(DSValue.createDSValue(dsValueStr)); } } return dsValues; }
From source file:org.oscarehr.decisionSupport.model.conditionValue.DSValue.java
private static int indexOfNotQuoted(String str, String query) { if (str.contains("'")) { Pattern stringQuotePattern = Pattern.compile("'.+?'"); Pattern allCharacters = Pattern.compile("."); String[] quotedStrings = stringQuotePattern.split(str); for (String quotedString : quotedStrings) { String blankedString = allCharacters.matcher(quotedString).replaceAll("'"); str = str.replace(quotedString, blankedString); }// w ww .j ava 2s. co m } return str.indexOf(query); }
From source file:org.opencastproject.util.data.functions.Strings.java
/** Create a split function from a regex pattern. */ public static Function<String, String[]> split(final Pattern splitter) { return new Function<String, String[]>() { @Override//from w w w . j a va 2 s . co m public String[] apply(String s) { return splitter.split(s); } }; }
From source file:de.forsthaus.backend.util.IpLocator.java
/** * Other than the getters & setters, this is the only method visible to the * outside world/*www. j a v a 2s . c o m*/ * * @param ip * The ip address to be located * @return IPLocator instance * @throws IOException * in case of any error/exception */ public static IpLocator locate(String ip) throws IOException { final String url = HOSTIP_LOOKUP_URL + ip; final URL u = new URL(url); final List<String> response = getContent(u); final Pattern splitterPattern = Pattern.compile(":"); final IpLocator ipl = new IpLocator(); for (final String token : response) { final String[] keyValue = splitterPattern.split(token); if (keyValue.length != 2) { continue; } final String key = StringUtils.upperCase(keyValue[0]); final String value = keyValue[1]; if (KEY_COUNTRY.equals(key)) { ipl.setCountry(value); } else if (KEY_CITY.equals(key)) { ipl.setCity(value); } else if (KEY_LATITUDE.equals(key)) { ipl.setLatitude(stringToFloat(value)); } else if (KEY_LONGITUDE.equals(key)) { ipl.setLongitude(stringToFloat(value)); } } return ipl; }