Example usage for java.util.regex Matcher matches

List of usage examples for java.util.regex Matcher matches

Introduction

In this page you can find the example usage for java.util.regex Matcher matches.

Prototype

public boolean matches() 

Source Link

Document

Attempts to match the entire region against the pattern.

Usage

From source file:Main.java

public static boolean isMobile(String mobile) {
    Matcher localMatcher = Pattern.compile("^1[3-8]+\\d{9}$").matcher(mobile);
    if ((!TextUtils.isEmpty(mobile)) && (localMatcher.matches())) {
        return true;
    }/* w  w  w.  jav  a  2 s. c om*/

    return false;
}

From source file:Main.java

/**
 * Parses an xs:duration attribute value, returning the parsed duration in milliseconds.
 *
 * @param value The attribute value to parse.
 * @return The parsed duration in milliseconds.
 *///ww w  .  j a va2 s.co m
public static long parseXsDuration(String value) {
    Matcher matcher = XS_DURATION_PATTERN.matcher(value);
    if (matcher.matches()) {
        boolean negated = !TextUtils.isEmpty(matcher.group(1));
        // Durations containing years and months aren't completely defined. We assume there are
        // 30.4368 days in a month, and 365.242 days in a year.
        String years = matcher.group(3);
        double durationSeconds = (years != null) ? Double.parseDouble(years) * 31556908 : 0;
        String months = matcher.group(5);
        durationSeconds += (months != null) ? Double.parseDouble(months) * 2629739 : 0;
        String days = matcher.group(7);
        durationSeconds += (days != null) ? Double.parseDouble(days) * 86400 : 0;
        String hours = matcher.group(10);
        durationSeconds += (hours != null) ? Double.parseDouble(hours) * 3600 : 0;
        String minutes = matcher.group(12);
        durationSeconds += (minutes != null) ? Double.parseDouble(minutes) * 60 : 0;
        String seconds = matcher.group(14);
        durationSeconds += (seconds != null) ? Double.parseDouble(seconds) : 0;
        long durationMillis = (long) (durationSeconds * 1000);
        return negated ? -durationMillis : durationMillis;
    } else {
        return (long) (Double.parseDouble(value) * 3600 * 1000);
    }
}

From source file:com.miserablemind.butter.helpers.Utilities.java

/**
 * Centralized method for the system to validate e-mail addresses. The pattern is {@code .+@.+\.[a-z]+}.
 *
 * @param email e-mail address to validate
 * @return {@code true} if e-mail address format is correct
 *//*from w  ww  . j a  va 2  s.c o  m*/
public static boolean isEmailValid(String email) {
    Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
    Matcher matcher = emailPattern.matcher(email);
    return matcher.matches();
}

From source file:Main.java

public static String parseCustomProxyClassName(String customProxyClassName, String originClassName,
        String targetClassName) {

    mCustomProxyClassName = customProxyClassName;

    String templete = "^(.*)(\\$[O|T])(.*)(\\$[O|T])(.*)$";
    Pattern compile = Pattern.compile(templete);
    Matcher matcher = compile.matcher(mCustomProxyClassName);
    if (!matcher.matches()) {
        printNotMatchErrorMessage();/*w w  w  .  ja v a2 s.  c  om*/
    }

    String group2 = matcher.group(2);
    String group4 = matcher.group(4);
    if (group2.matches("\\$[O]") && group4.matches("\\$[T]")) {
        group2 = originClassName;
        group4 = targetClassName;
    } else if (group2.matches("\\$[T]") && group4.matches("\\$[O]")) {
        group2 = targetClassName;
        group4 = originClassName;
    } else {
        printNotMatchErrorMessage();
    }

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(matcher.group(1));
    stringBuilder.append(group2);
    stringBuilder.append(matcher.group(3));
    stringBuilder.append(group4);
    stringBuilder.append(matcher.group(5));
    return stringBuilder.toString();
}

From source file:mitm.application.djigzo.james.mailets.SpecialAddress.java

public static SpecialAddress fromName(String name) {
    name = StringUtils.trimToNull(name);

    if (name != null) {
        Matcher matcher = PATTERN.matcher(name);

        if (matcher.matches()) {
            name = StringUtils.trimToNull(matcher.group(1));

            for (SpecialAddress specialAddress : SpecialAddress.values()) {
                if (specialAddress.name.equalsIgnoreCase(name)) {
                    return specialAddress;
                }/* ww  w.  j a  va  2  s. c  o m*/
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Obtains a list of files that live in the specified directory and match the glob pattern.
 *//*from ww w .  ja  v a  2 s  .com*/
public static String[] getFiles(File dir, String glob) {
    String regex = globToRegex(glob);
    final Pattern pattern = Pattern.compile(regex);
    String[] result = dir.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            Matcher matcher = pattern.matcher(name);
            return matcher.matches();
        }
    });
    Arrays.sort(result);

    return result;
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.brat.internal.model.BratEventArgument.java

public static BratEventArgument parse(String aLine) {
    Matcher m = PATTERN.matcher(aLine);

    if (!m.matches()) {
        throw new IllegalArgumentException("Illegal event argument format [" + aLine + "]");
    }/*from  w  ww. j a va2 s  . com*/

    int index = 0;
    if (StringUtils.isNumeric(m.group(INDEX))) {
        index = Integer.valueOf(m.group(INDEX));
    }

    return new BratEventArgument(m.group(SLOT), index, m.group(TARGET));
}

From source file:Main.java

/**
 * Extract the UUID part from a MusicBrainz identifier.
 * /*from  ww w.  j a v  a  2 s. c o  m*/
 * This function takes a MusicBrainz ID (an absolute URI) as the input
 * and returns the UUID part of the URI, thus turning it into a relative
 * URI. If <code>uriStr</code> is null or a relative URI, then it is
 * returned unchanged.
 * 
 * The <code>resType</code> parameter can be used for error checking.
 * Set it to 'artist', 'release', or 'track' to make sure 
 * <code>uriStr</code> is a syntactically valid MusicBrainz identifier
 * of the given resource type. If it isn't, an 
 * <code>IllegalArgumentException</code> exception is raised. This error
 * checking only works if <code>uriStr</code> is an absolute URI, of course.
 * 
 * Example:
 * >>>  MBUtils.extractUuid('http://musicbrainz.org/artist/c0b2500e-0cef-4130-869d-732b23ed9df5', 'artist')
 * 'c0b2500e-0cef-4130-869d-732b23ed9df5'
 * 
 * @param uriStr A string containing a MusicBrainz ID (an URI), or null
 * @param resType A string containing a resource type
 * @return A String containing a relative URI or null
 * @throws URISyntaxException 
 */
public static String extractUuid(String uriStr, String resType) {
    if (uriStr == null) {
        return null;
    }

    URI uri;
    try {
        uri = new URI(uriStr);
    } catch (URISyntaxException e) {
        return uriStr; // not really a valid URI, probably the UUID
    }
    if (uri.getScheme() == null) {
        return uriStr; // not really a valid URI, probably the UUID
    }

    if (!"http".equals(uri.getScheme()) || !"musicbrainz.org".equals(uri.getHost())) {
        throw new IllegalArgumentException(uri.toString() + " is no MB ID");
    }

    String regex = "^/(label|artist|release-group|release|recording|work|collection)/([^/]*)$";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(uri.getPath());
    if (m.matches()) {
        if (resType == null) {
            return m.group(2);
        } else {
            if (resType.equals(m.group(1))) {
                return m.group(2);
            } else {
                throw new IllegalArgumentException("expected '" + resType + "' Id");
            }
        }
    } else {
        throw new IllegalArgumentException("'" + uriStr + " is no valid MB id");
    }
}

From source file:net.mikaboshi.intra_mart.tools.log_stats.util.LogStringUtil.java

/**
 * URL?????//from w  ww . ja  va 2s. co  m
 * @param url
 * @return
 */
public static String truncateUrl(String url) {

    if (url == null) {
        return StringUtils.EMPTY;
    }

    Matcher matcher = TRUNCATE_URL_PATTERN.matcher(url);

    if (matcher.matches()) {
        return matcher.group(1);
    } else {
        return url;
    }
}

From source file:Main.java

/**
 * Expects an XML xs:dateTime lexical format string, as in
 * <code>2005-10-24T11:57:31.000+01:00</code>. Some bad MXML files miss
 * timezone or milliseconds information, thus a certain amount of tolerance
 * is applied towards malformed timestamp string representations. If
 * unparseable, this method will return <code>null</code>.
 * //w  ww  .java  2  s  . com
 * @param xmlLexicalString
 * @return
 */
public static Date parseXsDateTime(String xsDateTime) {
    // match pattern against timestamp string
    Matcher matcher = xsDtPattern.matcher(xsDateTime);
    if (matcher.matches() == true) {
        // extract data particles from matched groups / subsequences
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2)) - 1;
        int day = Integer.parseInt(matcher.group(3));
        int hour = Integer.parseInt(matcher.group(4));
        int minute = Integer.parseInt(matcher.group(5));
        int second = Integer.parseInt(matcher.group(6));
        int millis = 0;
        // probe for successful parsing of milliseconds
        if (matcher.group(7) != null) {
            millis = Integer.parseInt(matcher.group(8));
        }
        cal.set(year, month, day, hour, minute, second);
        cal.set(GregorianCalendar.MILLISECOND, millis);
        String tzString = matcher.group(9);
        if (tzString != null) {
            // timezone matched
            tzString = "GMT" + tzString.replace(":", "");
            cal.setTimeZone(TimeZone.getTimeZone(tzString));
        } else {
            cal.setTimeZone(TimeZone.getTimeZone("GMT"));
        }
        return cal.getTime();
    } else {
        return null;
    }
}