List of usage examples for java.util.regex Matcher reset
public Matcher reset(CharSequence input)
From source file:PopClean.java
public static void main(String args[]) { try {/* ww w .java 2 s . co m*/ 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:Main.java
public static String[] parseStringList(String list) { // final Pattern patWs = Pattern.compile("\\s+"); final Matcher matchWs = Pattern.compile("[^\\s]+").matcher(""); matchWs.reset(list); LinkedList<String> matchList = new LinkedList<String>(); while (matchWs.find()) { matchList.add(matchWs.group());// w w w . ja v a 2 s .c om } String[] retArr = new String[matchList.size()]; return (String[]) matchList.toArray(retArr); }
From source file:MatcherResetCharSequenceExample.java
public static void test() { String output = ""; Pattern p = Pattern.compile("\\d"); Matcher m1 = p.matcher("01234"); while (m1.find()) { System.out.println("\t\t" + m1.group()); }/*from w w w. ja v a2 s .co m*/ //now reset the matcher with new data m1.reset("56789"); System.out.println("After resetting the Matcher"); //iterate through the matcher while (m1.find()) { System.out.println("\t\t" + m1.group()); } }
From source file:com.cloudera.sqoop.lib.BlobRef.java
/** * Create a BlobRef based on parsed data from a line of text. * This only operates correctly on external blobs; inline blobs are simply * returned as null. You should store BLOB data in SequenceFile format * if reparsing is necessary./* w w w . j a v a 2s .c o m*/ * @param inputString the text-based input data to parse. * @return a new BlobRef containing a reference to an external BLOB, or * an empty BlobRef if the data to be parsed is actually inline. */ public static BlobRef parse(String inputString) { // If inputString is of the form 'externalLob(lf,%s,%d,%d)', then this is // an external BLOB stored at the LobFile indicated by '%s' with the next // two arguments representing its offset and length in the file. // Otherwise, it is an inline BLOB, which we don't support parsing of. Matcher m = EXTERNAL_MATCHER.get(); m.reset(inputString); if (m.matches()) { // This is a LobFile. Extract the filename, offset and len from the // matcher. return new BlobRef(m.group(1), Long.valueOf(m.group(2)), Long.valueOf(m.group(3))); } else { // This is inline BLOB string data. LOG.warn("Reparsing inline BLOB data is not supported; use SequenceFiles."); return new BlobRef(); } }
From source file:com.github.parzonka.esa.indexing.LuceneIndexer.java
private static boolean matches(Matcher matcher, String string) { matcher.reset(string); return matcher.matches(); }
From source file:org.apache.sqoop.lib.BlobRef.java
/** * Create a BlobRef based on parsed data from a line of text. * This only operates correctly on external blobs; inline blobs are simply * returned as null. You should store BLOB data in SequenceFile format * if reparsing is necessary.//from w w w . ja v a2s .c o m * @param inputString the text-based input data to parse. * @return a new BlobRef containing a reference to an external BLOB, or * an empty BlobRef if the data to be parsed is actually inline. */ public static com.cloudera.sqoop.lib.BlobRef parse(String inputString) { // If inputString is of the form 'externalLob(lf,%s,%d,%d)', then this is // an external BLOB stored at the LobFile indicated by '%s' with the next // two arguments representing its offset and length in the file. // Otherwise, it is an inline BLOB, which we don't support parsing of. Matcher m = org.apache.sqoop.lib.LobRef.EXTERNAL_MATCHER.get(); m.reset(inputString); if (m.matches()) { // This is a LobFile. Extract the filename, offset and len from the // matcher. return new com.cloudera.sqoop.lib.BlobRef(m.group(1), Long.valueOf(m.group(2)), Long.valueOf(m.group(3))); } else { // This is inline BLOB string data. LOG.warn("Reparsing inline BLOB data is not supported; use SequenceFiles."); return new com.cloudera.sqoop.lib.BlobRef(); } }
From source file:net.sourceforge.pmd.util.FileUtil.java
/** * Handy method to find a certain pattern into a file. While this method * lives in the FileUtils, it was designed with with unit test in mind (to * check result redirected into a file)// ww w. j a v a2s .c o m * * @param file * @param pattern * @return */ public static boolean findPatternInFile(final File file, final String pattern) { Pattern regexp = Pattern.compile(pattern); Matcher matcher = regexp.matcher(""); FileIterable it = new FileIterable(file); for (String line : it) { matcher.reset(line); // reset the input if (matcher.find()) { return true; } } return false; }
From source file:org.apache.hadoop.hive.common.type.HiveIntervalDayTime.java
public static HiveIntervalDayTime valueOf(String strVal) { HiveIntervalDayTime result = null;/*from w ww . jav a 2 s . c o m*/ if (strVal == null) { throw new IllegalArgumentException("Interval day-time string was null"); } Matcher patternMatcher = PATTERN_MATCHER.get(); patternMatcher.reset(strVal); if (patternMatcher.matches()) { // Parse out the individual parts try { // Sign - whether interval is positive or negative int sign = 1; String field = patternMatcher.group(1); if (field != null && field.equals("-")) { sign = -1; } int days = sign * IntervalDayTimeUtils.parseNumericValueWithRange("day", patternMatcher.group(2), 0, Integer.MAX_VALUE); byte hours = (byte) (sign * IntervalDayTimeUtils.parseNumericValueWithRange("hour", patternMatcher.group(3), 0, 23)); byte minutes = (byte) (sign * IntervalDayTimeUtils.parseNumericValueWithRange("minute", patternMatcher.group(4), 0, 59)); int seconds = 0; int nanos = 0; field = patternMatcher.group(5); if (field != null) { BigDecimal bdSeconds = new BigDecimal(field); if (bdSeconds.compareTo(IntervalDayTimeUtils.MAX_INT_BD) > 0) { throw new IllegalArgumentException("seconds value of " + bdSeconds + " too large"); } seconds = sign * bdSeconds.intValue(); nanos = sign * bdSeconds.subtract(new BigDecimal(bdSeconds.toBigInteger())) .multiply(IntervalDayTimeUtils.NANOS_PER_SEC_BD).intValue(); } result = new HiveIntervalDayTime(days, hours, minutes, seconds, nanos); } catch (Exception err) { throw new IllegalArgumentException("Error parsing interval day-time string: " + strVal, err); } } else { throw new IllegalArgumentException( "Interval string does not match day-time format of 'd h:m:s.n': " + strVal); } return result; }
From source file:org.apache.tika.cli.BatchCommandLineBuilder.java
/** * Take the input args and separate them into args that belong on the commandline * and those that belong as jvm args for the child process. * @param args -- literal args from TikaCLI commandline * @param commandLine args that should be part of the batch commandline * @param jvmArgs args that belong as jvm arguments for the child process *///from ww w . j a v a 2 s . c om private static void mapifyArgs(final String[] args, final Map<String, String> commandLine, final Map<String, String> jvmArgs) { if (args.length == 0) { return; } Matcher matcher = JVM_OPTS_PATTERN.matcher(""); for (int i = 0; i < args.length; i++) { if (matcher.reset(args[i]).find()) { String jvmArg = matcher.group(1) + matcher.group(2); String v = ""; if (i < args.length - 1 && !args[i + 1].startsWith("-")) { v = args[i + 1]; i++; } jvmArgs.put(jvmArg, v); } else if (args[i].startsWith("-")) { String k = args[i]; String v = ""; if (i < args.length - 1 && !args[i + 1].startsWith("-")) { v = args[i + 1]; i++; } commandLine.put(k, v); } } }
From source file:GrepSun.java
private static void grep(File f, CharBuffer cb) { Matcher lm = linePattern.matcher(cb); // Line matcher Matcher pm = null; // Pattern matcher int lines = 0; while (lm.find()) { lines++;/*w w w . j a v a 2 s .c o m*/ CharSequence cs = lm.group(); // The current line if (pm == null) pm = pattern.matcher(cs); else pm.reset(cs); if (pm.find()) System.out.print(f + ":" + lines + ":" + cs); if (lm.end() == cb.limit()) break; } }