/** * Sums the line length without the line separation and the characters which matched the line separation pattern * @param scanner the scanner from which to read the line's length * @return the length of the line with the line separator. */ private int getLineLengthWithLineSeparator(final Scanner scanner) { int lineLength = scanner.nextLine().length(); final String lineSeparationMatch = scanner.match().group(1); if (lineSeparationMatch != null) { lineLength += lineSeparationMatch.length(); } return lineLength; }
/** * Sums the line length without the line separation and the characters which matched the line separation pattern * @param scanner the scanner from which to read the line's length * @return the length of the line with the line separator. */ private int getLineLengthWithLineSeparator(final Scanner scanner) { int lineLength = scanner.nextLine().length(); final String lineSeparationMatch = scanner.match().group(1); if (lineSeparationMatch != null) { lineLength += lineSeparationMatch.length(); } return lineLength; }
public static void main(String[] args) { Scanner scanner = new Scanner(threatData); String pattern = "(\\d+[.]\\d+[.]\\d+[.]\\d+)@" + "(\\d{2}/\\d{2}/\\d{4})"; while(scanner.hasNext(pattern)) { scanner.next(pattern); MatchResult match = scanner.match(); String ip = match.group(1); String date = match.group(2); System.out.format( "Threat on %s from %s%n", date,ip); } } }
String text = "$abc$and$xyz$"; Scanner sc = new Scanner(text); while (sc.findInLine("\\$([^$]*)\\$") != null) { System.out.println(sc.match().group(1)); } // abc, xyz
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)); }
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 */ } }
Scanner kbd = new Scanner(System.in); kbd.useDelimiter(System.getProperty("line.separator")); //you could create a rule automatically with some kind of a rules engine if(kbd.hasNext("create(\\s+)(\\d+)(\\s+)(\\d+)(\\s+)(fast|flexible)(\\s+)(fast|flexible)")) { MatchResult res = kbd.match(); for(int i = 0; i <= res.groupCount(); ++i) { System.out.println(res.group(i)); //handleRules(res.group(i), i); } } kbd.close();
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(); }
String str = "X-Value = -0.525108, Y-Value = 7.746691, Z-Value = 5.863008, Timestamp(milliseconds) = 23001"; Scanner scanner = new Scanner(str); if (scanner.findInLine("^X-Value\\s*=\\s*([^,]*),\\s*Y-Value\\s*=\\s*([^,]*),\\s*Z-Value\\s*=\\s*([^,]*),\\s*Timestamp\\(milliseconds\\)\\s+=\\s+([^,]*)\\s*$") != null) { MatchResult result = scanner.match(); System.out.printf("x=[%s]; y=[%s]; z=[%s]; ts=[%s]%n", result.group(1), result.group(2), result.group(3), result.group(4)); } scanner.close();
Scanner s = new Scanner(file); Pattern pattern = Pattern.compile("(.+?)\n(.+?)\n(.+?)\n(.+?)\n"); while(s.findWithinHorizon(pattern, 0) != null) { MatchResult result = s.match(); city = result.group(1); temp = result.group(2); wind = result.group(3); img = result.group(4); // etc. (Note that the next pass through the loop will override these variables // so, unless you save the values somewhere, you will end up with them holding last // entry from your file once the loop is done. }
String input = "User clientId=287372.Dstid=129 Some more text clientId=336263. Dstid=2451. This clientNum=120. Some more text clientId=12345. Dstid=2421."; Scanner s = new Scanner(input); Pattern p = Pattern.compile("(clientId=)(\\d+).[ ]*(Dstid=)(\\d+)"); while(s.findInLine(p)!=null) { MatchResult res = s.match(); System.out.println("clint id : " +res.group(2)); System.out.println("Dst id : " + res.group(4)); } s.close();
Scanner s = new Scanner(new File("text")); Pattern p = Pattern.compile("^(\\d+) (\\d+) (\\d+)$", Pattern.MULTILINE); while(s.hasNextLine()){ if(s.findInLine(p)!=null){ //just printing the result. you can do needful here. MatchResult result = s.match(); System.out.println("full line:" + result.group(0)); System.out.println("individuals"); for (int i=1; i<=result.groupCount(); i++) System.out.println(result.group(i)); } s.nextLine(); }
Scanner s = new Scanner(commandOutput); String pattern = ".*?Address: (\\S*) .*?Channel:(\\d*) .*?Frequency:(\\S*) .*?Quality=(\\d*)/(\\d*) .*?Signal level=-(\\d*)"; //instead of "true, use hasNextLine()" while(s.hasNextLine()){ s.findInLine(pattern); MatchResult result = s.match(); for (int i = 1; i <= result.groupCount(); i++) { System.out.println(result.group(i)); } s.nextLine(); // consume everything from this line }
String input = "fish1-1 fish2-2"; java.util.Scanner s = new java.util.Scanner(input); s.findInLine("(\\d+)"); java.util.regex.MatchResult result = s.match(); for (int i=1; i<=result.groupCount(); i++) System.out.println(result.group(i)); s.close();
String input = "1 fish 2 fish red fish blue fish"; Scanner s = new Scanner(input); s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"); MatchResult result = s.match(); for (int i=1; i<=result.groupCount(); i++) System.out.println(result.group(i)); s.close(); `
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 }; }
private List<GherkinLineSpan> getSpans(String delimiter) { List<GherkinLineSpan> lineSpans = new ArrayList<GherkinLineSpan>(); Scanner scanner = new Scanner(trimmedLineText).useDelimiter(delimiter); while (scanner.hasNext()) { String cell = scanner.next(); int column = scanner.match().start() + indent() + 1; lineSpans.add(new GherkinLineSpan(column, cell)); } return lineSpans; } }
private static String extractImageName( String header ) { Vector<Pair<String, String>> allTokenData = extractTokenData( header, "BSB" ); String tokenData = allTokenData.get( 0 ).second( ); Scanner s = new Scanner( tokenData ); s.findWithinHorizon( "NA=([\\w;\\s]+)", tokenData.length( ) ); String imageName = s.match( ).group( 1 ); s.close( ); return imageName; }
private static Stream<MatchResult> matches(Scanner s, String pattern) { Pattern compiled=Pattern.compile(pattern); return StreamSupport.stream( new Spliterators.AbstractSpliterator<MatchResult>(1000, Spliterator.ORDERED|Spliterator.NONNULL) { @Override public boolean tryAdvance(Consumer<? super MatchResult> action) { if(s.findWithinHorizon(compiled, 0)==null) return false; action.accept(s.match()); return true; } }, false); }