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:de.codesourcery.eve.skills.datamodel.EveFlags.java

public static void main(String[] args) {

    final Pattern PATTERN = Pattern.compile("^([0-9]+),(.*?),(.*)$");

    for (String line : FLAGS.split("\n")) {
        final String trimmed = StringUtils.strip(line);

        final Matcher m = PATTERN.matcher(line);
        if (!m.matches()) {
            throw new RuntimeException();
        }/*w  w  w .j  a v a  2  s .  co  m*/

        final int id = Integer.parseInt(m.group(1));
        final String name = m.group(2).toUpperCase().replaceAll(" ", "_");
        final String displayName = StringUtils.strip(m.group(3));
        System.out.println(name + "(" + id + ",\"" + displayName + "\"),");
    }
}

From source file:StartEnd.java

public static void main(String[] args) {
    String[] input = new String[] { "Java has regular expressions in 1.4",
            "regular expressions now expressing in Java", "Java represses oracular expressions" };
    Pattern p1 = Pattern.compile("re\\w*"), p2 = Pattern.compile("Java.*");
    for (int i = 0; i < input.length; i++) {
        System.out.println("input " + i + ": " + input[i]);
        Matcher m1 = p1.matcher(input[i]), m2 = p2.matcher(input[i]);
        while (m1.find())
            System.out.println("m1.find() '" + m1.group() + "' start = " + m1.start() + " end = " + m1.end());
        while (m2.find())
            System.out.println("m2.find() '" + m2.group() + "' start = " + m2.start() + " end = " + m2.end());
        if (m1.lookingAt()) // No reset() necessary
            System.out.println("m1.lookingAt() start = " + m1.start() + " end = " + m1.end());
        if (m2.lookingAt())
            System.out.println("m2.lookingAt() start = " + m2.start() + " end = " + m2.end());
        if (m1.matches()) // No reset() necessary
            System.out.println("m1.matches() start = " + m1.start() + " end = " + m1.end());
        if (m2.matches())
            System.out.println("m2.matches() start = " + m2.start() + " end = " + m2.end());
    }/*from   ww  w.  j av  a 2s.  c o m*/
}

From source file:com.meidusa.amoeba.route.AbstractQueryRouter.java

License:asdf

public static void main(String[] aa) {
    String[] aaa = StringUtil.split("asdfasdf,asdf;aqwer", ";,");
    for (String aaaaa : aaa) {
        System.out.println(aaaaa);
    }/*from w w  w  .  java2s .co m*/
    System.out.println(System.currentTimeMillis());
    String source = "^fileSys_[a-zA-Z0-9_]*";
    Pattern pattern = null;
    if (pattern == null) {
        pattern = Pattern.compile(source);
    }
    java.util.regex.Matcher matcher = pattern.matcher("fileSys_abc12d");
    System.out.println(matcher.matches());
}

From source file:com.basistech.lucene.tools.LuceneQueryTool.java

public static void main(String[] args)
        throws IOException, org.apache.lucene.queryparser.classic.ParseException {
    String charsetName = Charset.defaultCharset().name();
    if (!"UTF-8".equals(charsetName)) {
        // Really only a problem on mac, where the default charset is MacRoman,
        // and it cannot be changed via the system Locale.
        System.err.println(String.format("defaultCharset is %s, but we require UTF-8.", charsetName));
        System.err.println("Set -Dfile.encoding=UTF-8 on the Java command line, or");
        System.err.println("set JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 in the environment.");
        System.exit(1);//from   www .  j a  v  a 2 s  .  c o m
    }

    Options options = LuceneQueryTool.createOptions();
    CommandLineParser parser = new GnuParser();
    CommandLine cmdline = null;
    try {
        cmdline = parser.parse(options, args);
        validateOptions(options, args);
    } catch (org.apache.commons.cli.ParseException e) {
        System.err.println(e.getMessage());
        usage(options);
        System.exit(1);
    }
    String[] remaining = cmdline.getArgs();
    if (remaining != null && remaining.length > 0) {
        System.err.println("unknown extra args found: " + Lists.newArrayList(remaining));
        usage(options);
        System.exit(1);
    }

    String[] indexPaths = cmdline.getOptionValues("index");
    IndexReader[] readers = new IndexReader[indexPaths.length];
    for (int i = 0; i < indexPaths.length; i++) {
        readers[i] = DirectoryReader.open(FSDirectory.open(new File(indexPaths[i])));

    }
    IndexReader reader = new MultiReader(readers, true);

    LuceneQueryTool that = new LuceneQueryTool(reader);

    String opt;
    opt = cmdline.getOptionValue("query-limit");
    if (opt != null) {
        that.setQueryLimit(Integer.parseInt(opt));
    }
    opt = cmdline.getOptionValue("output-limit");
    if (opt != null) {
        that.setOutputLimit(Integer.parseInt(opt));
    }
    opt = cmdline.getOptionValue("analyzer");
    if (opt != null) {
        that.setAnalyzer(opt);
    }
    opt = cmdline.getOptionValue("query-field");
    if (opt != null) {
        that.setDefaultField(opt);
    }
    opt = cmdline.getOptionValue("output");
    PrintStream out = null;
    if (opt != null) {
        out = new PrintStream(new FileOutputStream(new File(opt)), true);
        that.setOutputStream(out);
    }
    if (cmdline.hasOption("show-id")) {
        that.setShowId(true);
    }
    if (cmdline.hasOption("show-hits")) {
        that.setShowHits(true);
    }
    if (cmdline.hasOption("show-score")) {
        that.setShowScore(true);
    }
    if (cmdline.hasOption("sort-fields")) {
        that.setSortFields(true);
    }
    if (cmdline.hasOption("suppress-names")) {
        that.setSuppressNames(true);
    }
    if (cmdline.hasOption("tabular")) {
        that.setTabular(true);
    }

    String[] opts;
    opts = cmdline.getOptionValues("fields");
    if (opts != null) {
        that.setFieldNames(Lists.newArrayList(opts));
    }
    opt = cmdline.getOptionValue("regex");
    if (opt != null) {
        Pattern p = Pattern.compile("^(.*?):/(.*)/$");
        Matcher m = p.matcher(opt);
        if (m.matches()) {
            that.setRegex(m.group(1), Pattern.compile(m.group(2)));
        } else {
            System.err.println("Invalid regex, should be field:/regex/");
            usage(options);
            System.exit(1);
        }
    }
    opts = cmdline.getOptionValues("query");
    that.run(opts);
    if (out != null) {
        out.close();
    }
    reader.close();
}

From source file:PopClean.java

public static void main(String args[]) {
    try {/*from  ww  w  . jav a 2  s.  com*/
        String hostname = null, username = null, password = null;
        int port = 110;
        int sizelimit = -1;
        String subjectPattern = null;
        Pattern pattern = null;
        Matcher matcher = null;
        boolean delete = false;
        boolean confirm = true;

        // Handle command-line arguments
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-user"))
                username = args[++i];
            else if (args[i].equals("-pass"))
                password = args[++i];
            else if (args[i].equals("-host"))
                hostname = args[++i];
            else if (args[i].equals("-port"))
                port = Integer.parseInt(args[++i]);
            else if (args[i].equals("-size"))
                sizelimit = Integer.parseInt(args[++i]);
            else if (args[i].equals("-subject"))
                subjectPattern = args[++i];
            else if (args[i].equals("-debug"))
                debug = true;
            else if (args[i].equals("-delete"))
                delete = true;
            else if (args[i].equals("-force")) // don't confirm
                confirm = false;
        }

        // Verify them
        if (hostname == null || username == null || password == null || sizelimit == -1)
            usage();

        // Make sure the pattern is a valid regexp
        if (subjectPattern != null) {
            pattern = Pattern.compile(subjectPattern);
            matcher = pattern.matcher("");
        }

        // Say what we are going to do
        System.out
                .println("Connecting to " + hostname + " on port " + port + " with username " + username + ".");
        if (delete) {
            System.out.println("Will delete all messages longer than " + sizelimit + " bytes");
            if (subjectPattern != null)
                System.out.println("that have a subject matching: [" + subjectPattern + "]");
        } else {
            System.out.println("Will list subject lines for messages " + "longer than " + sizelimit + " bytes");
            if (subjectPattern != null)
                System.out.println("that have a subject matching: [" + subjectPattern + "]");
        }

        // If asked to delete, ask for confirmation unless -force is given
        if (delete && confirm) {
            System.out.println();
            System.out.print("Do you want to proceed (y/n) [n]: ");
            System.out.flush();
            BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
            String response = console.readLine();
            if (!response.equals("y")) {
                System.out.println("No messages deleted.");
                System.exit(0);
            }
        }

        // Connect to the server, and set up streams
        s = new Socket(hostname, port);
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

        // Read the welcome message from the server, confirming it is OK.
        System.out.println("Connected: " + checkResponse());

        // Now log in
        send("USER " + username); // Send username, wait for response
        send("PASS " + password); // Send password, wait for response
        System.out.println("Logged in");

        // Check how many messages are waiting, and report it
        String stat = send("STAT");
        StringTokenizer t = new StringTokenizer(stat);
        System.out.println(t.nextToken() + " messages in mailbox.");
        System.out.println("Total size: " + t.nextToken());

        // Get a list of message numbers and sizes
        send("LIST"); // Send LIST command, wait for OK response.
        // Now read lines from the server until we get . by itself
        List msgs = new ArrayList();
        String line;
        for (;;) {
            line = in.readLine();
            if (line == null)
                throw new IOException("Unexpected EOF");
            if (line.equals("."))
                break;
            msgs.add(line);
        }

        // Now loop through the lines we read one at a time.
        // Each line should specify the message number and its size.
        int nummsgs = msgs.size();
        for (int i = 0; i < nummsgs; i++) {
            String m = (String) msgs.get(i);
            StringTokenizer st = new StringTokenizer(m);
            int msgnum = Integer.parseInt(st.nextToken());
            int msgsize = Integer.parseInt(st.nextToken());

            // If the message is too small, ignore it.
            if (msgsize <= sizelimit)
                continue;

            // If we're listing messages, or matching subject lines
            // find the subject line for this message
            String subject = null;
            if (!delete || pattern != null) {
                subject = getSubject(msgnum); // get the subject line

                // If we couldn't find a subject, skip the message
                if (subject == null)
                    continue;

                // If this subject does not match the pattern, then
                // skip the message
                if (pattern != null) {
                    matcher.reset(subject);
                    if (!matcher.matches())
                        continue;
                }

                // If we are listing, list this message
                if (!delete) {
                    System.out.println("Subject " + msgnum + ": " + subject);
                    continue; // so we never delete it
                }
            }

            // If we were asked to delete, then delete the message
            if (delete) {
                send("DELE " + msgnum);
                if (pattern == null)
                    System.out.println("Deleted message " + msgnum);
                else
                    System.out.println("Deleted message " + msgnum + ": " + subject);
            }
        }

        // When we're done, log out and shutdown the connection
        shutdown();
    } catch (Exception e) {
        // If anything goes wrong print exception and show usage
        System.err.println(e);
        usage();
        // Always try to shutdown nicely so the server doesn't hang on us
        shutdown();
    }
}

From source file:PatternMethodExample.java

public static void reusePatternMethodExample() {
    Pattern p = Pattern.compile("\\d");
    Matcher matcher = p.matcher("5");
    boolean isOk = matcher.matches();
    System.out.println("original pattern matches " + isOk);

    String tmp = p.pattern();/*from w  ww.  j  a v  a 2  s  .c  o  m*/
    Pattern p2 = Pattern.compile(tmp);
    matcher = p.matcher("5");
    isOk = matcher.matches();
    System.out.println("second pattern matches " + isOk);
}

From source file:Main.java

public static boolean isValidCharacters(String s) {
    Pattern p = Pattern.compile("^[0-9a-zA-Z]{6,9}$");
    Matcher m = p.matcher(s);
    return m.matches();
}

From source file:Main.java

public static boolean isValidTagAndAlias(String s) {
    Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|]+$");
    Matcher m = p.matcher(s);
    return m.matches();
}

From source file:Main.java

public static boolean isQQ(String qq) {
    Pattern p = Pattern.compile("^[0-9]{4,11}$");
    Matcher m = p.matcher(qq);
    return m.matches();
}

From source file:Main.java

public static boolean isTel(String tel) {
    Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
    Matcher m = p.matcher(tel);
    return m.matches();
}