/** * Compiles the pattern string and tries to find a substring matching it in the input data. The * delimiter will be ignored. This is the same as invoking * {@code findInLine(Pattern.compile(pattern))}. * * @param pattern * a string used to construct a pattern which is in turn used to * match a substring of the input data. * @return the matched string or {@code null} if the pattern is not found * before the next line terminator. * @throws IllegalStateException * if the {@code Scanner} is closed. * @see #findInLine(Pattern) */ public String findInLine(String pattern) { return findInLine(Pattern.compile(pattern)); }
Scanner sc = new Scanner("abc"); char ch = sc.findInLine(".").charAt(0); System.out.println(ch); // prints "a" System.out.println(sc.next()); // prints "bc"
Scanner s = new Scanner(System.in); char c = s.findInLine(".").charAt(0); int n = s.nextInt();
protected String expandProperties(final String input) { String output = input; Scanner s = new Scanner(output); String foundInLine; while ((foundInLine = s.findInLine("(\\$[\\{]\\w+[\\}])")) != null) { String envProperty = getEnvProperty(extractKey(foundInLine)); envProperty = envProperty != null ? envProperty : ""; output = output.replace(foundInLine, envProperty); } return output; } }
private long getTimestampFromStdout(Scanner scanner) { String line = scanner.findInLine(".*timestamp\\sis\\:\\s(\\d+.*)"); String[] parts = line.split(" "); return Long.parseLong(parts[parts.length - 1]); }
private long getWallClockTimestamp(Scanner scanner) { String line = scanner.findInLine(".*Wall\\sclock\\sdatetime.*\\s(\\d+.*)"); String[] parts = line.split(" "); return ISODateTimeFormat.dateTime().parseDateTime(parts[parts.length - 1]).getMillis(); }
String input = "<div class=\"some\">Here is a sample text<br /><p>A paragraph here</p></div><ul><li>List Item</li></ul><6>"; Scanner scanner = new Scanner(input); String result = ""; while( (result = scanner.findInLine("<\\w+")) !=null ){ System.out.println(result); }
String text = "There's \"a man\" that live next door " + "'in my neighborhood', \"and he gets me down...\""; Scanner sc = new Scanner(text); Pattern pattern = Pattern.compile( "\"[^\"]*\"" + "|'[^']*'" + "|[A-Za-z']+" ); String token; while ((token = sc.findInLine(pattern)) != null) { System.out.println("[" + token + "]"); }
Scanner s = new Scanner(...); s.useDelimiter("[^\\w]"); // Find an angle bracket token, if one is next. String token = s.findInLine("<[^.]*>"); if (token != null) // process angle bracket token token = s.next(); // get next delimited token ...
Scanner sc = new Scanner(inputString); String match; while ((match = sc.findInLine("(\\d+)"))!=null) { // here i print it, but you need to put it into an array (like above), // i'll leave it to you as an exercise System.out.println(Integer.parseInt(match)); } sc.close();
Scanner inputTaker = new Scanner(System.in); String input = inputTaker.nextLine(); Scanner scanner = new Scanner(input); char someCharacter = scanner.findInLine(".").charAt(0); // Do something with character
private static String extractDuration (String s) { Matcher matcher = DURATION_LINE_PATTERN.matcher (s); if ( matcher.find () ) { String line = matcher.group (); Scanner scanner = new Scanner (line); return scanner.findInLine (DUR_PATTERN); } return null; } }
public static void main(String[] args) { String str = "RoomA3814221"; String pattern = "[0-9]+"; Scanner sc = new Scanner(str); String result = sc.findInLine(pattern); String[] arr = result.split(""); System.out.println(result); System.out.println(arr.length); for (String s: arr) { System.out.println(s); } }
Scanner s = new Scanner("07/04/2012 <DEBUG> a=1, b=foo, c=bar : d=erere m=abcd hello world!"); Pattern p = Pattern.compile("(?<=\\s|,)[^\\s=]+=[^,\\s]+"); String out; while((out = s.findInLine(p))!=null) { System.out.println(Arrays.toString(out.split("="))); }
String text = "$abc$and$xyz$"; Scanner sc = new Scanner(text); while (sc.findInLine("\\$([^$]*)\\$") != null) { System.out.println(sc.match().group(1)); } // abc, xyz
String pattern = "\\s*a"; Scanner scanner1 = new Scanner(" \t a"); Scanner scanner2 = new Scanner(" \t\n a"); System.out.printf(">%s<\n\n", scanner1.findInLine(pattern)); System.out.printf(">%s<", scanner2.findWithinHorizon(pattern, 0));
private void scanFileForDescription(File file) { try (Scanner scan = new Scanner(file, StandardCharsets.UTF_8.name())) { scan.findInLine(":name\\s*=\\s*(.*)\\s+:description\\s*=\\s*(.*)"); MatchResult results = scan.match(); mScriptName = results.group(1).trim(); mDescription = results.group(2).trim(); } catch (IllegalStateException e) { /* bad luck */ } catch (FileNotFoundException e) { /* ignore - it should not happen here */ } }
Pattern toMatch = Pattern.compile("(\\D*[a-zA-Z]+)(\\d*)(\\D*)") Scanner sc = new Scanner(headerText); MatchResult res; try { sc.findInLine(toMatch); res = sc.match(); } finally { sc.close(); }
@Test public void testSweepTable() throws Exception { try (SingleBackendCliTestRunner runner = makeRunner( paramsWithDryRunSet(SWEEP_COMMAND, "-t", TABLE_ONE.getQualifiedName()))) { TestAtlasDbServices services = runner.connect(moduleFactory); SerializableTransactionManager txm = services.getTransactionManager(); TimestampService tss = services.getTimestampService(); KeyValueService kvs = services.getKeyValueService(); createTable(kvs, TABLE_ONE, TableMetadataPersistence.SweepStrategy.CONSERVATIVE); createTable(kvs, TABLE_TWO, TableMetadataPersistence.SweepStrategy.CONSERVATIVE); long ts1 = put(txm, TABLE_ONE, "foo", "bar"); long ts2 = put(txm, TABLE_TWO, "foo", "tar"); long ts3 = put(txm, TABLE_ONE, "foo", "baz"); long ts4 = put(txm, TABLE_TWO, "foo", "taz"); long ts5 = tss.getFreshTimestamp(); String stdout = sweep(runner, ts5); Scanner scanner = new Scanner(stdout); final long cellValuesExamined = Long.parseLong(scanner.findInLine("\\d+ cell values").split(" ")[0]); final long deletedCells = Long.parseLong( scanner.findInLine("deleted \\d+ stale versions of those cells").split(" ")[1]); Assert.assertEquals(2, cellValuesExamined); Assert.assertEquals(1, deletedCells); Assert.assertEquals("baz", get(kvs, TABLE_ONE, "foo", ts5)); Assert.assertEquals(deletedValue("bar"), get(kvs, TABLE_ONE, "foo", mid(ts1, ts3))); Assert.assertEquals(ImmutableSet.of(deletedTimestamp(ts1), ts3), getAllTs(kvs, TABLE_ONE, "foo")); Assert.assertEquals("taz", get(kvs, TABLE_TWO, "foo", ts5)); Assert.assertEquals("tar", get(kvs, TABLE_TWO, "foo", mid(ts3, ts4))); Assert.assertEquals(ImmutableSet.of(ts2, ts4), getAllTs(kvs, TABLE_TWO, "foo")); } }
private String expandProperties(final String input) { String output = input; Scanner s = new Scanner(output); String foundInLine = null; while ( (foundInLine = s.findInLine("(\\$[\\{]\\w+[\\}])")) != null) { String envProperty = getEnvProperty(extractKey(foundInLine)); envProperty = envProperty != null ? envProperty : ""; output = output.replace(foundInLine, envProperty); } return output; }