Example usage for java.util Scanner findWithinHorizon

List of usage examples for java.util Scanner findWithinHorizon

Introduction

In this page you can find the example usage for java.util Scanner findWithinHorizon.

Prototype

public String findWithinHorizon(Pattern pattern, int horizon) 

Source Link

Document

Attempts to find the next occurrence of the specified pattern.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("ID:", 0);

    if (sc.hasNext())
        System.out.println(sc.next());
    else/*from w  ww  . j av a 2s .  c o m*/
        System.out.println("Error!");
}

From source file:MainClass.java

public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("IDs:", 0);

    if (sc.hasNext())
        System.out.println(sc.next());
    else//from  w  ww  . j a  v a2 s  . c om
        System.out.println("Error!");
}

From source file:Main.java

public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("ID:", 100);

    if (sc.hasNext())
        System.out.println(sc.next());
    else/* ww  w .j  a va2  s  .  co  m*/
        System.out.println("Error!");
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    // find a string of world, with horizon of 10
    System.out.println(scanner.findWithinHorizon("com", 10));

    // find a string of world, with horizon of 20
    System.out.println(scanner.findWithinHorizon("=", 20));

    // print the rest of the string
    System.out.println(scanner.nextLine());

    scanner.close();//from w  ww . j a v a2s .co m
}

From source file:Main.java

public static void main(String[] args) {

    String s = "java2s.com 1 + 1 = 2.0";

    Scanner scanner = new Scanner(s);

    // find a pattern of 2 letters before com, with horizon of 5
    System.out.println(scanner.findWithinHorizon(Pattern.compile("..com"), 5));

    // find a pattern of 2 letters before com, with horizon of 10
    System.out.println(scanner.findWithinHorizon(Pattern.compile("..com"), 10));

    // print the rest of the string
    System.out.println(scanner.nextLine());

    scanner.close();//  www.j  ava2  s. co m
}

From source file:Main.java

public static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon)
        throws Exception {
    InputStream in = null;/*from  www  .  j a  va 2  s.  co m*/
    try {
        final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();

        in = process.getInputStream();
        final Scanner scanner = new Scanner(in);

        final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null;
        if (matchFound) {
            return scanner.match();
        } else {
            throw new Exception();
        }
    } catch (final IOException e) {
        throw new Exception(e);
    }
}

From source file:Main.java

private static ArrayList<String> getMatches(InputStream in, Pattern pattern) {
    ArrayList<String> matches = new ArrayList<String>();
    Scanner scanner = new Scanner(in, "UTF-8");
    String match = "";
    while (match != null) {
        match = scanner.findWithinHorizon(pattern, 0);
        if (match != null) {
            matches.add(scanner.match().group(2));
        }//from  w w w .j ava 2 s  .  co m
    }

    return matches;
}

From source file:com.google.mr4c.util.CustomFormat.java

private static List<String> extractNames(String pattern) {
    List<String> names = new ArrayList<String>();
    Scanner scanner = new Scanner(pattern);
    while (scanner.findWithinHorizon(VAR_REGEX, 0) != null) {
        MatchResult result = scanner.match();
        String val = result.group(1);
        names.add(val);
    }/*w  ww  . j  a va2 s  .  c  o m*/
    return names;
}

From source file:com.google.mr4c.util.CustomFormat.java

public boolean matches(String str) {
    Scanner scanner = new Scanner(str);
    String check = scanner.findWithinHorizon(m_regex, 0);
    return str.equals(check);
}

From source file:com.google.mr4c.util.CustomFormat.java

public Map<String, String> parse(String str) {
    if (!matches(str)) {
        throw new IllegalArgumentException(String.format("[%s] doesn't match pattern [%s]", str, m_pattern));
    }//from w ww  .  j  a va2 s .  c  o  m
    Scanner scanner = new Scanner(str);
    scanner.findWithinHorizon(m_regex, 0);
    MatchResult result = scanner.match();
    Map<String, String> vals = new HashMap<String, String>();
    if (result.groupCount() != m_nameList.size()) {
        // this shouldn't be able to happen
        throw new IllegalStateException(
                String.format("[%s] doesn't match pattern [%s]; found %d matches, expected %d", str, m_pattern,
                        result.groupCount(), m_nameList.size()));
    }
    for (int i = 1; i <= result.groupCount(); i++) {
        String name = m_nameList.get(i - 1);
        String val = result.group(i);
        if (vals.containsKey(name)) {
            if (!vals.get(name).equals(val)) {
                throw new IllegalArgumentException(
                        String.format("[%s]doesnt match pattern [%s]; variable [%s] has values [%s] and [%s]",
                                str, m_pattern, name, val, vals.get(name)));
            }
        }
        vals.put(name, result.group(i));
    }
    return vals;
}