Example usage for java.util.regex Pattern matches

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

Introduction

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

Prototype

public static boolean matches(String regex, CharSequence input) 

Source Link

Document

Compiles the given regular expression and attempts to match the given input against it.

Usage

From source file:Main.java

public static int getCPUCores() {
    //Strategy 1.
    //The value may less than real value.
    //int cores_runtime = Runtime.getRuntime().availableProcessors();

    //Strategy 2.
    int cores = 1;
    class CpuFilter implements FileFilter {
        @Override/*from   ww w.  j  a  v a  2  s. c o  m*/
        public boolean accept(File pathname) {
            return Pattern.matches("cpu[0-9]", pathname.getName());
        }
    }
    try {
        File dir = new File("/sys/devices/system/cpu/");
        File[] files = dir.listFiles(new CpuFilter());
        cores = files.length;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return cores;
}

From source file:Main.java

/**
 * Gets the number of cores available in this device, across all processors.
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
 *
 * @return The number of cores, or 1 if failed to get result
 */// w  ww .j ava2  s.c om
public static int getNumCores() {
    //Private Class to display only CPU devices in the directory listing
    class CpuFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            //Check if filename is "cpu", followed by a single digit number
            if (Pattern.matches("cpu[0-9]+", pathname.getName())) {
                return true;
            }
            return false;
        }
    }

    try {
        //Get directory containing CPU info
        File dir = new File("/sys/devices/system/cpu/");
        //Filter to only list the devices we care about
        File[] files = dir.listFiles(new CpuFilter());
        //Return the number of cores (virtual CPU devices)
        return files.length;
    } catch (Exception e) {
        //Default to return 1 core
        return 1;
    }
}

From source file:Main.java

public static boolean checkPostcode(String postcode) {
    String regex = "[1-9]\\d{5}";
    return Pattern.matches(regex, postcode);
}

From source file:Main.java

public static boolean isBlank(String s) {
    if (s == null) {
        return true;
    }//from   ww w  .  j  a v  a  2  s.c o  m
    return Pattern.matches("\\s*", s);
}

From source file:Main.java

/** 
 * Gets the number of cores available in this device, across all processors. 
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu" 
 * @return The number of cores, or 1 if failed to get result 
 *///from   w  w  w .  ja  v a 2 s.c om
public static int getNumCores() {
    try {
        //Get directory containing CPU info 
        File dir = new File("/sys/devices/system/cpu/");
        //Filter to only list the devices we care about 
        File[] files = dir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                //Check if filename is "cpu", followed by a single digit number 
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }

        });
        //Return the number of cores (virtual CPU devices) 
        return files.length;
    } catch (Exception e) {
        //Default to return 1 core 
        return 1;
    }
}

From source file:Main.java

public static boolean isUrl(String s) {
    if (s == null) {
        return false;
    }/*from w  w  w  .  j  a v  a  2  s. c  o m*/
    return Pattern.matches(URL_REG_EXPRESSION, s);
}

From source file:Main.java

/**
 * Implementation of the 'Like' operation.
 * /*from  w ww  .j a  v  a  2s .  c o  m*/
 * @param string  string to compare
 * @param pattern  regular expression pattern
 * @return  {@code true} if the the string matches the pattern, {@code false}
 *          otherwise
 */
public static boolean like(String string, String pattern) {
    return Pattern.matches(pattern, string);
}

From source file:Main.java

public static boolean isEmail(String s) {
    if (TextUtils.isEmpty(s)) {
        return false;
    }/*from  w  w  w. ja va  2s .  com*/
    return Pattern.matches(EMAIL_REG_EXPRESSION, s);
}

From source file:Main.java

/**
 * Helper method to validate the phone number
 *//*from w  w  w. j a  va2  s .  co m*/
public static boolean isPhoneNumberValid(CharSequence phoneNumber) {
    boolean isGoodPhone = false;
    if (!Pattern.matches("[a-zA-Z]+", phoneNumber)) {
        isGoodPhone = !(phoneNumber.length() < 5 || phoneNumber.length() > 14);
    } else {
        isGoodPhone = false;
    }
    return isGoodPhone;
}

From source file:Main.java

/**
 * Gets the number of cores available in this device, across all processors.
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
 * @return The number of cores, or 1 if failed to get result
 *//*ww w  .  j a  v  a  2s.  c  o m*/
public static int getNumCores() {
    try {
        //Get directory containing CPU info
        File dir = new File("/sys/devices/system/cpu/");
        //Filter to only list the devices we care about
        File[] files = dir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                //Check if filename is "cpu", followed by a single digit number
                return Pattern.matches("cpu[0-9]", pathname.getName());
            }

        });
        //Return the number of cores (virtual CPU devices)
        return files.length;
    } catch (Exception e) {
        //Default to return 1 core
        return 1;
    }
}