/** * Tries to find the pattern in the input between the current position and the specified * {@code horizon}. Delimiters are ignored. This call is the same as invoking * {@code findWithinHorizon(Pattern.compile(pattern))}. * * @param pattern * the pattern used to scan. * @param horizon * the search limit. * @return the matched string, or {@code null} if the pattern is not found * within the specified horizon. * @throws IllegalStateException * if the {@code Scanner} is closed. * @throws IllegalArgumentException * if {@code horizon} is less than zero. * @see #findWithinHorizon(Pattern, int) */ public String findWithinHorizon(String pattern, int horizon) { return findWithinHorizon(Pattern.compile(pattern), horizon); }
/** * Determines if the given file contains the given regular expression. * * @param file the file to test * @param pattern the pattern used to test the file * @return <code>true</code> if the regular expression matches the file * content; otherwise <code>false</code> * @throws IOException thrown if there is an error reading the file */ public static boolean contains(File file, String pattern) throws IOException { try (Scanner fileScanner = new Scanner(file)) { final Pattern regex = Pattern.compile(pattern); if (fileScanner.findWithinHorizon(regex, 0) != null) { return true; } } return false; }
/** * Extracts the package name from an XmlTree dump of AndroidManifest.xml by the <code>aapt</code> tool. * * @param aaptDumpXmlTree output from <code>aapt dump xmltree <apkFile> AndroidManifest.xml * @return the package name from inside the apkFile. */ protected String extractPackageNameFromAndroidManifestXmlTree( String aaptDumpXmlTree ) { final Scanner scanner = new Scanner( aaptDumpXmlTree ); // Finds the root element named "manifest". scanner.findWithinHorizon( "^E: manifest", 0 ); // Finds the manifest element's attribute named "package". scanner.findWithinHorizon( " A: package=\"", 0 ); // Extracts the package value including the trailing double quote. String packageName = scanner.next( ".*?\"" ); // Removes the double quote. packageName = packageName.substring( 0, packageName.length() - 1 ); return packageName; }
/** * Determines if the given file contains the given regular expressions. * * @param file the file to test * @param patterns the array of patterns used to test the file * @return <code>true</code> if one of the regular expressions matches the * file content; otherwise <code>false</code> * @throws IOException thrown if there is an error reading the file */ public static boolean contains(File file, String[] patterns) throws IOException { final List<Pattern> regexes = new ArrayList<>(); for (String pattern : patterns) { regexes.add(Pattern.compile(pattern)); } try (Scanner fileScanner = new Scanner(file)) { for (Pattern regex : regexes) { if (fileScanner.findWithinHorizon(regex, 0) != null) { return true; } } } return false; } }
/** * Returns true if there is a line terminator in the input. * This method may block. * * @throws IllegalStateException if this {@code Scanner} is closed. */ public boolean hasNextLine() { saveCurrentStatus(); String result = findWithinHorizon(LINE_PATTERN, 0); recoverPreviousStatus(); return result != null; }
Scanner s = new Scanner(new File("thefile")); String nextMatch = s.findWithinHorizon(yourPattern, 0);
String dur = sc.findWithinHorizon(durPattern, 0); if (dur == null) throw new RuntimeException("Could not parse duration."); while (null != (match = sc.findWithinHorizon(timePattern, 0))) { double progress = Double.parseDouble(match) / totalSecs; System.out.printf("Progress: %.2f%%%n", progress * 100);
try (Scanner in = new Scanner(Paths.get(INFILE), "UTF-8")) { String line; while ((line = in.findWithinHorizon(pat, 0)) != null) { // Process the line, then write the output using something like // FileWriter.write(String) that doesn't add another line terminator. } }
public boolean streamContainsString(Reader reader, String searchString) throws IOException { Scanner streamScanner = new Scanner(reader); if (streamScanner.findWithinHorizon(searchString, 0) != null) { return true; } else { return false; } }
Scanner sc = new Scanner("xxyyxxy"); for (int n = 0;; n++) { String s = sc.findWithinHorizon("y", 0); if (s == null) { System.out.println(n); break; } }
Pattern newLinePattern = Pattern.compile("(\\r\\n?|\\n)", Pattern.MULTILINE); String sourceString = "\r\n\n\r\r\n\n"; Scanner scan = new Scanner(sourceString); int count = 0; while (scan.findWithinHorizon(newLinePattern, 0) != null) { count++; } System.out.println("found "+count+" newlines"); // finds 5 newlines
char b; Scanner sc = new Scanner(System.in); outer: do{ b= sc.findWithinHorizon(".", 0).charAt(0); System.out.println(b); if(b=='c'||b=='C'){break outer;} }while(true);
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));
Scanner sc = new Scanner(new File("test.txt"), "UTF-8"); Pattern p = Pattern.compile( "<span[^>]*class=\"filename\"[^>]*>\\s*<a[^>]*href=\"([^\"]+)\"" ); while (sc.findWithinHorizon(p, 0) != null) { MatchResult m = sc.match(); System.out.println(m.group(1)); }
Scanner scanner = new Scanner(is).useDelimiter("\\n"); scanner.findWithinHorizon(Pattern.compile("Block\\scount:\\s*(\\d+)"), 0); String blockCount = scanner.match().group(1); scanner.findWithinHorizon(Pattern.compile("Free\\sblocks:\\s*(\\d+)"), 0); String freeBlocks = scanner.match().group(1);
private static int[] extractDimension( String header ) { Vector<Pair<String, String>> allTokenData = extractTokenData( header, "BSB" ); String tokenData = allTokenData.get( 0 ).second( ); Scanner s = new Scanner( tokenData ); s.findWithinHorizon( "RA\\s*=\\s*(\\d+)\\s*,\\s*(\\d+)", tokenData.length( ) ); MatchResult results = s.match( ); int width_PIXELS = Integer.parseInt( results.group( 1 ) ); int height_PIXELS = Integer.parseInt( results.group( 2 ) ); s.close( ); return new int[] { width_PIXELS, height_PIXELS }; }
/** * Returns true if there is a line terminator in the input. * This method may block. * * @throws IllegalStateException if this {@code Scanner} is closed. */ public boolean hasNextLine() { saveCurrentStatus(); String result = findWithinHorizon(LINE_PATTERN, 0); recoverPreviousStatus(); return result != null; }
/** * Returns true if there is a line terminator in the input. * This method may block. * * @throws IllegalStateException if this {@code Scanner} is closed. */ public boolean hasNextLine() { saveCurrentStatus(); String result = findWithinHorizon(LINE_PATTERN, 0); recoverPreviousStatus(); return result != null; }
/** * Returns true if there is a line terminator in the input. * This method may block. * * @throws IllegalStateException if this {@code Scanner} is closed. */ public boolean hasNextLine() { saveCurrentStatus(); String result = findWithinHorizon(LINE_PATTERN, 0); recoverPreviousStatus(); return result != null; }
/** * Returns true if there is a line terminator in the input. * This method may block. * * @throws IllegalStateException if this {@code Scanner} is closed. */ public boolean hasNextLine() { saveCurrentStatus(); String result = findWithinHorizon(LINE_PATTERN, 0); recoverPreviousStatus(); return result != null; }