List of usage examples for java.util.regex Pattern compile
public static Pattern compile(String regex, int flags)
From source file:BGrep.java
public static void main(String[] args) { String encodingName = "UTF-8"; // Default to UTF-8 encoding int flags = Pattern.MULTILINE; // Default regexp flags try { // Fatal exceptions are handled after this try block // First, process any options int nextarg = 0; while (args[nextarg].charAt(0) == '-') { String option = args[nextarg++]; if (option.equals("-e")) { encodingName = args[nextarg++]; } else if (option.equals("-i")) { // case-insensitive matching flags |= Pattern.CASE_INSENSITIVE; } else if (option.equals("-s")) { // Strict Unicode processing flags |= Pattern.UNICODE_CASE; // case-insensitive Unicode flags |= Pattern.CANON_EQ; // canonicalize Unicode } else { System.err.println("Unknown option: " + option); usage();/*from w ww .jav a2 s .c o m*/ } } // Get the Charset for converting bytes to chars Charset charset = Charset.forName(encodingName); // Next argument must be a regexp. Compile it to a Pattern object Pattern pattern = Pattern.compile(args[nextarg++], flags); // Require that at least one file is specified if (nextarg == args.length) usage(); // Loop through each of the specified filenames while (nextarg < args.length) { String filename = args[nextarg++]; CharBuffer chars; // This will hold complete text of the file try { // Handle per-file errors locally // Open a FileChannel to the named file FileInputStream stream = new FileInputStream(filename); FileChannel f = stream.getChannel(); // Memory-map the file into one big ByteBuffer. This is // easy but may be somewhat inefficient for short files. ByteBuffer bytes = f.map(FileChannel.MapMode.READ_ONLY, 0, f.size()); // We can close the file once it is is mapped into memory. // Closing the stream closes the channel, too. stream.close(); // Decode the entire ByteBuffer into one big CharBuffer chars = charset.decode(bytes); } catch (IOException e) { // File not found or other problem System.err.println(e); // Print error message continue; // and move on to the next file } // This is the basic regexp loop for finding all matches in a // CharSequence. Note that CharBuffer implements CharSequence. // A Matcher holds state for a given Pattern and text. Matcher matcher = pattern.matcher(chars); while (matcher.find()) { // While there are more matches // Print out details of the match System.out.println(filename + ":" + // file name matcher.start() + ": " + // character pos matcher.group()); // matching text } } } // These are the things that can go wrong in the code above catch (UnsupportedCharsetException e) { // Bad encoding name System.err.println("Unknown encoding: " + encodingName); } catch (PatternSyntaxException e) { // Bad pattern System.err.println("Syntax error in search pattern:\n" + e.getMessage()); } catch (ArrayIndexOutOfBoundsException e) { // Wrong number of arguments usage(); } }
From source file:core.plugin.mybatis.PageInterceptor.java
public static void main(String[] args) { List<String> tests = new ArrayList<String>(); tests.add("select count(*) from abc \n\t\t where\n abc"); tests.add("SELECT COUNT(*) from abc"); tests.add(" select count (*) from abc"); tests.add(" select count( *) from abc"); tests.add("select count( * ),id from abc"); tests.add("select * from abc"); tests.add("select abc,test,fdas from abc"); tests.add("select count(adb) from abc"); tests.add("select count(0) from abc"); tests.add("select min(count(*)) from abc"); tests.add("update min(count(*)) from abc"); tests.add("delete min(count(*)) from abc"); Pattern p1 = Pattern.compile(SQL_SELECT_REGEX, Pattern.DOTALL | Pattern.CASE_INSENSITIVE); Pattern p2 = Pattern.compile(SQL_COUNT_REGEX, Pattern.DOTALL | Pattern.CASE_INSENSITIVE); for (String str : tests) { Matcher m1 = p1.matcher(str); Matcher m2 = p2.matcher(str); System.out.println("?: " + str); System.out.println(" select?? " + m1.matches()); System.out.println(" count?? " + m2.matches()); System.out.println();/* w w w. ja v a 2 s .c o m*/ } }
From source file:Main.java
public static Pattern getUrlPattern() { return Pattern.compile( "^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$", Pattern.CASE_INSENSITIVE); }
From source file:Main.java
public static boolean isLinkAvailable(String link) { Pattern pattern = Pattern.compile( "^(http://|https://)?((?:[A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\\.)+([A-Za-z]+)[/\\?\\:]?.*$", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(link); return matcher.matches(); }
From source file:Main.java
public static String getCsFromRE(String pt, String content) { Pattern pa1 = Pattern.compile(pt, Pattern.CASE_INSENSITIVE); Matcher matcher1 = pa1.matcher(content); if (matcher1.find()) { return matcher1.group(1); } else {/*w w w .j a v a 2s .c om*/ return ""; } }
From source file:Main.java
public static String delPreLocation(String regex, String content) { Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(content); if (matcher.find()) { return content.substring(matcher.end(), content.length()); }//from w w w . j av a2 s . c om return content; }
From source file:Main.java
public static String readValue(String whole, String key) { Pattern pattern = Pattern.compile(key + "=([\"'])(.+?)\\1", Pattern.CASE_INSENSITIVE); Matcher mat = pattern.matcher(whole); if (mat.find()) { return mat.group(2); } else {/*from ww w.ja va 2 s . co m*/ pattern = Pattern.compile(key + "=([^ \t\r\n\f\b\"'/>]+?)[ \t\r\n\f\b\"'/>]", Pattern.CASE_INSENSITIVE); mat = pattern.matcher(whole); if (mat.find()) { return mat.group(1); } } return ""; }
From source file:Main.java
/** Validate email address **/ public static boolean validateEmail(String email) { Pattern p = Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(email); return m.matches(); }
From source file:Main.java
/** Validate telephone number **/ public static boolean validatePhone(String phone, int length) { Pattern p = Pattern.compile(String.format("[0-9]{%d}", length), Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(phone); return m.matches(); }