List of usage examples for java.lang String matches
public boolean matches(String regex)
From source file:edu.harvard.mcz.nametools.AuthorNameComparator.java
/** * Test to see if an authorship string appears to contain a year * between 1000 and the current century. * /*from w ww .j av a 2 s .co m*/ * @param authorship string to test for a year. * * @return true if a four digit number is found. */ public static boolean calculateHasYear(String authorship) { boolean result = false; if (authorship != null && authorship.matches(".*[12][0-9]{3}.*") && authorship.replaceAll("[^0-9]", "").length() == 4) { result = true; } return result; }
From source file:cn.org.awcp.core.utils.DateUtils.java
/** * ?<br>//from w w w . ja va 2 s. c om * ??<br> * generate by: vakin jiang at 2012-3-1 * * @param dateStr * @return */ public static Date parseDate(String dateStr) { SimpleDateFormat format = null; if (StringUtils.isBlank(dateStr)) { return null; } String _dateStr = dateStr.trim(); try { if (_dateStr.matches("\\d{1,2}[A-Z]{3}")) { _dateStr = _dateStr + (Calendar.getInstance().get(Calendar.YEAR) - 2000); } // 01OCT12 if (_dateStr.matches("\\d{1,2}[A-Z]{3}\\d{2}")) { format = new SimpleDateFormat("ddMMMyy", Locale.ENGLISH); } else if (_dateStr.matches("\\d{1,2}[A-Z]{3}\\d{4}.*")) {// 01OCT2012 // ,01OCT2012 // 1224,01OCT2012 // 12:24 _dateStr = _dateStr.replaceAll("[^0-9A-Z]", "").concat("000000").substring(0, 15); format = new SimpleDateFormat("ddMMMyyyyHHmmss", Locale.ENGLISH); } else { StringBuffer sb = new StringBuffer(_dateStr); String[] tempArr = _dateStr.split("\\s+"); tempArr = tempArr[0].split("-|\\/"); if (tempArr.length == 3) { if (tempArr[1].length() == 1) { sb.insert(5, "0"); } if (tempArr[2].length() == 1) { sb.insert(8, "0"); } } _dateStr = sb.append("000000").toString().replaceAll("[^0-9]", "").substring(0, 14); if (_dateStr.matches("\\d{14}")) { format = new SimpleDateFormat("yyyyMMddHHmmss"); } } Date date = format.parse(_dateStr); return date; } catch (Exception e) { throw new RuntimeException("?[" + dateStr + "]"); } }
From source file:Main.java
public static boolean isValidDuration(String value) { // voodoo regex for checking valid xsd:duration string. See // http://www.w3.org/TR/xmlschema-2/#duration for details. String regex = "-?P((\\d)+Y)?((\\d)+M)?((\\d)+D)?((T(\\d)+H((\\d)+M)?((\\d)+(\\.(\\d)+)?S)?)|(T(\\d)+M((\\d)+(\\.(\\d)+)?S)?)|(T(\\d)+(\\.(\\d)+)?S))?"; return value.length() > 1 && value.matches(regex); }
From source file:com.fjn.helper.common.io.file.common.FileUtil.java
/** * ?????\/:*?">|</* ww w . ja v a 2 s . c o m*/ * \\\\ \ * / / * : : * \\* * * \\? ? * \" " * > > * < < * \\| | * * * @param filename * @return true ???false?? */ public static boolean checkFileName(String filename) { boolean noExist = true; // noExist:? noExist = filename.matches("^[^\\\\/:\\*\\?\">\\|<]+(\\.[^\\\\/:\\*\\?\">\\|<]+)?$"); return noExist; }
From source file:com.formkiq.core.util.Strings.java
/** * String has at least 1 letter./*from w ww . j av a 2 s . co m*/ * @param s {@link String} * @return boolean */ public static boolean hasAtLeast1Letter(final String s) { return s.matches(".*[a-zA-Z]+.*"); }
From source file:Main.java
public static String parseTelephoneNumber(String sel) { if (sel == null || sel.length() == 0) return null; // Hack: Remove trailing left-to-right mark (Google Maps adds this) if (sel.codePointAt(sel.length() - 1) == 8206) { sel = sel.substring(0, sel.length() - 1); }//from ww w . jav a2 s . c o m String number = null; if (sel.matches("([Tt]el[:]?)?\\s?[+]?(\\(?[0-9|\\s|\\-|\\.]\\)?)+")) { String elements[] = sel.split("([Tt]el[:]?)"); number = elements.length > 1 ? elements[1] : elements[0]; number = number.replace(" ", ""); // Remove option (0) in international numbers, e.g. +44 (0)20 ... if (number.matches("\\+[0-9]{2,3}\\(0\\).*")) { int openBracket = number.indexOf('('); int closeBracket = number.indexOf(')'); number = number.substring(0, openBracket) + number.substring(closeBracket + 1); } } return number; }
From source file:com.gatf.executor.core.GatfFunctionHandler.java
public static String handleFunction(String function) { if (function.equals(BOOLEAN)) { Random rand = new Random(); return String.valueOf(rand.nextBoolean()); } else if (function.matches(DT_FMT_REGEX)) { Matcher match = datePattern.matcher(function); match.matches();//from w w w . ja v a2 s. co m SimpleDateFormat format = new SimpleDateFormat(match.group(1)); return format.format(new Date()); } else if (function.matches(DT_FUNC_FMT_REGEX)) { Matcher match = specialDatePattern.matcher(function); match.matches(); String formatStr = match.group(1); String operation = match.group(2); int value = Integer.valueOf(match.group(3)); String unit = match.group(4); SimpleDateFormat format = new SimpleDateFormat(formatStr); try { Date dt = format.parse(format.format(new Date())); Calendar cal = Calendar.getInstance(); cal.setTime(dt); value = (operation.equals(MINUS) ? -value : value); if (unit.equals(YEAR)) { cal.add(Calendar.YEAR, value); } else if (unit.equals(MONTH)) { cal.add(Calendar.MONTH, value); } else if (unit.equals(DAY)) { cal.add(Calendar.DAY_OF_YEAR, value); } else if (unit.equals(HOUR)) { cal.add(Calendar.HOUR_OF_DAY, value); } else if (unit.equals(MINUITE)) { cal.add(Calendar.MINUTE, value); } else if (unit.equals(SECOND)) { cal.add(Calendar.SECOND, value); } else if (unit.equals(MILLISECOND)) { cal.add(Calendar.MILLISECOND, value); } return format.format(cal.getTime()); } catch (Exception e) { throw new AssertionError("Invalid date format specified - " + formatStr); } } else if (function.equals(FLOAT)) { Random rand = new Random(12345678L); return String.valueOf(rand.nextFloat()); } else if (function.equals(ALPHA)) { return RandomStringUtils.randomAlphabetic(10); } else if (function.equals(ALPHANUM)) { return RandomStringUtils.randomAlphanumeric(10); } else if (function.equals(NUMBER_PLUS)) { Random rand = new Random(); return String.valueOf(rand.nextInt(1234567)); } else if (function.equals(NUMBER_MINUS)) { Random rand = new Random(); return String.valueOf(-rand.nextInt(1234567)); } else if (function.equals(NUMBER)) { Random rand = new Random(); boolean bool = rand.nextBoolean(); return bool ? String.valueOf(rand.nextInt(1234567)) : String.valueOf(-rand.nextInt(1234567)); } else if (function.matches(RANDOM_RANGE_REGEX)) { Matcher match = randRangeNum.matcher(function); match.matches(); String min = match.group(1); String max = match.group(2); try { int nmin = Integer.parseInt(min); int nmax = Integer.parseInt(max); return String.valueOf(nmin + (int) (Math.random() * ((nmax - nmin) + 1))); } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:com.ocs.dynamo.utils.DateUtils.java
/** * Checks if a string represents a valid week code (yyyy-ww) * //www . ja v a 2 s. c o m * @param weekCode * the week code * @return */ public static boolean isValidWeekCode(String weekCode) { if (weekCode == null) { return true; } // pattern must match if (!weekCode.matches(WEEK_CODE_PATTERN)) { return false; } int year = getYearFromWeekCode(weekCode); int week = getWeekFromWeekCode(weekCode); int lastWeekOfYear = getLastWeekOfYear(year); return FIRST_WEEK_NUMBER <= week && week <= lastWeekOfYear; }
From source file:interpolation.Polyfit.java
public static ArrayList<Pair<Integer, Double>> loadsimple(final File file) { final ArrayList<Pair<Integer, Double>> points = new ArrayList<Pair<Integer, Double>>(); final ArrayList<Pair<Integer, Double>> normalpoints = new ArrayList<Pair<Integer, Double>>(); try {//w w w. j a va 2 s .c om BufferedReader in = Util.openFileRead(file); while (in.ready()) { String line = in.readLine().trim(); while (line.contains("\t\t")) line = line.replaceAll("\t\t", "\t"); if (line.length() >= 3 && line.matches("[0-9].*")) { final String[] split = line.trim().split("\t"); final int frame = (int) Double.parseDouble(split[0]); final double length = Double.parseDouble(split[1]); points.add(new ValuePair<Integer, Double>(frame, length)); } } } catch (Exception e) { e.printStackTrace(); return null; } double maxlength = Double.MIN_VALUE; for (Pair<Integer, Double> point : points) { double length = point.getB(); if (length > maxlength) maxlength = length; } for (Pair<Integer, Double> point : points) { Pair<Integer, Double> newpoint = new ValuePair<Integer, Double>(point.getA(), point.getB()); normalpoints.add(newpoint); } return normalpoints; }
From source file:org.archive.modules.fetcher.AbstractCookieStorage.java
/** * Load cookies. The input is text in the Netscape's 'cookies.txt' file * format. Example entry of cookies.txt file: * <p>// w ww . jav a 2 s .c o m * www.archive.org FALSE / FALSE 1311699995 details-visit texts-cralond * </p> * <p> * Each line has 7 tab-separated fields: * </p> * <ol> * <li>DOMAIN: The domain that created and have access to the cookie value.</li> * <li>FLAG: A TRUE or FALSE value indicating if hosts within the given * domain can access the cookie value.</li> * <li>PATH: The path within the domain that the cookie value is valid for.</li> * <li>SECURE: A TRUE or FALSE value indicating if to use a secure * connection to access the cookie value.</li> * <li>EXPIRATION: The expiration time of the cookie value, or -1 for no * expiration</li> * <li>NAME: The name of the cookie value</li> * <li>VALUE: The cookie value</li> * </ol> * * @param reader * input * @param cookiesFile * file in the Netscape's 'cookies.txt' format. */ public static void loadCookies(Reader reader, SortedMap<String, Cookie> cookies) { BufferedReader br = new BufferedReader(reader); try { String line; int lineNo = 1; while ((line = br.readLine()) != null) { if (!line.matches("\\s*(?:#.*)?")) { // skip blank links and comments String[] tokens = line.split("\\t"); if (tokens.length == 7) { long epochSeconds = Long.parseLong(tokens[4]); Date expirationDate = (epochSeconds >= 0 ? new Date(epochSeconds * 1000) : null); Cookie cookie = new Cookie(tokens[0], tokens[5], tokens[6], tokens[2], expirationDate, Boolean.valueOf(tokens[3]).booleanValue()); cookie.setDomainAttributeSpecified(Boolean.valueOf(tokens[1]).booleanValue()); LOGGER.fine("Adding cookie: domain " + cookie.getDomain() + " cookie " + cookie.toExternalForm()); cookies.put(cookie.getSortKey(), cookie); } else { LOGGER.warning( "cookies input line " + lineNo + " invalid, expected 7 tab-delimited tokens"); } } lineNo++; } } catch (IOException e) { LOGGER.log(Level.WARNING, e.getMessage(), e); } }