Example usage for java.util Scanner findInLine

List of usage examples for java.util Scanner findInLine

Introduction

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

Prototype

public String findInLine(Pattern pattern) 

Source Link

Document

Attempts to find the next occurrence of the specified pattern ignoring delimiters.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    String instr = "Name: Joe Age: 28 ID: 77";

    Scanner conin = new Scanner(instr);

    conin.findInLine("Age:"); // find Age

    if (conin.hasNext())
        System.out.println(conin.next());
    else// w  w  w . j  a  v  a2s.  c  om
        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 "com"
    System.out.println(scanner.findInLine("com"));

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

    scanner.close();/*from w  w w  .j  a  v a  2 s . c  o 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 any letter plus "com"
    System.out.println(scanner.findInLine(Pattern.compile(".com")));

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

    scanner.close();//  w w  w.j  a v a  2s  .c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    String userName;//from www  . ja  va 2s .  c  om
    System.out.println("Enter your email address: ");
    userName = scn.findInLine(Pattern.compile("[a-z]+"));

    System.out.println(userName);
}

From source file:jp.go.nict.langrid.servicesupervisor.frontend.FrontEnd.java

private static String getJsonRpcCallTree(InputStream body) throws IOException {
    Scanner s = new Scanner(body, "UTF-8");
    String tok = s.findInLine("\"name\":\"calltree\",\"namespace\":\""
            + StringEscapeUtils.escapeJavaScript(LangridConstants.ACTOR_SERVICE_CALLTREE)
            + "\",\"value\":\"(\\[[a-zA-Z0-9_\\[\\]\":,\\.\\{\\}\\\\]+\\])\"");
    if (tok != null) {
        return StringEscapeUtils.unescapeJavaScript(s.match().group(1));
    }//from  ww  w .ja va 2s  .co m
    return "";
}

From source file:com.jaxio.celerio.template.VelocityGeneratorTest.java

@Test
public void testExtraction() {
    String message = "Object 'com.jaxio.celerio.convention.WellKnownFolder' does not contain property 'resource' at src/main/resources/spring/springmvc-parent.p.vm.xml[line "
            + "28, column 47]";
    Scanner s = new Scanner(message);
    String u = s.findInLine("\\[line (\\d+), column (\\d+)\\]");
    assertThat(u).isNotEmpty();/* w  ww .  j av a  2 s.c  o m*/
    MatchResult result = s.match();
    assertThat(result.groupCount()).isEqualTo(2);
    assertThat(result.group(1)).isEqualTo("28");
    assertThat(result.group(2)).isEqualTo("47");
}

From source file:demo.vmware.commands.CommandProcessor.java

/**
 * /*from  w w w  .j  av  a  2s.co  m*/
 * @param s
 * @return next token that could have been quoted string with quotes removed
 */
String getNextToken(Scanner s) {
    String nextToken = s.findInLine(UGLY_PARSING_PATTERN);
    if (nextToken == null) {
        return nextToken;
    } else {
        return nextToken.replace("\"", "");
    }
}

From source file:com.jaxio.celerio.template.VelocityGenerator.java

private void displayLinesInError(VelocityException exception, TemplatePack templatePack, Template template) {
    try {/*from  w  w  w .  j  a  v  a  2  s.  com*/
        Scanner scanner = new Scanner(exception.getMessage());
        String match = scanner.findInLine("\\[line (\\d+), column (\\d+)\\]");
        if (match == null) {
            return;
        }
        MatchResult result = scanner.match();
        int lineInError = parseInt(result.group(1));
        int column = parseInt(result.group(2));

        String[] lines = template.getTemplate().split("\\n");

        int linesBeforeToDisplay = 2;
        int linesAfterToDisplay = 2;

        for (int i = max(0, lineInError - linesBeforeToDisplay); i < lineInError; i++) {
            System.err.println(prefix(templatePack, template, i + 1) + lines[i]);
        }
        String prefix = prefix(templatePack, template, lineInError);
        System.err.print(prefix);
        for (int i = 0; i < column - 1; i++) {
            System.err.print(" ");
        }
        System.err.println("^");
        for (int i = lineInError; i < min(lines.length - 1, lineInError + linesAfterToDisplay); i++) {
            System.err.println(prefix(templatePack, template, i + 1) + lines[i]);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:reittienEtsinta.Kayttoliittyma.java

public void kaynnista() {
    Scanner lukija = new Scanner(System.in);
    System.out.println("Komennot: \n" + " koord <lon> <lat> <lon> <lat> <tiedostonimi> \n "
            + "hae <lahtosolmu> <maalisolmu> <tiedostonimi> \n " + "kaari <lahtosolmu> <maalisolmu>\n"
            + "lopeta");
    while (true) {
        String komento = lukija.nextLine();
        if (komento.substring(0, 5).equals("koord")) {
            Scanner komentotulkki = new Scanner(komento);

            double lonA = Double.parseDouble(komentotulkki.findInLine("[0-9]{6}"));
            double latA = Double.parseDouble(komentotulkki.findInLine("[0-9]{7}"));
            double lonM = Double.parseDouble(komentotulkki.findInLine("[0-9]{6}"));
            double latM = Double.parseDouble(komentotulkki.findInLine("[0-9]{7}"));

            this.haeReittiKoordinaateilla(latA, lonA, latM, lonM);

            String polku = komentotulkki.next();
            this.tallennaReitti(polku);
        } else if (komento.substring(0, 3).equals("hae")) {
            Scanner komentotulkki = new Scanner(komento);
            int alku = Integer.parseInt(komentotulkki.findInLine("[0-9]{1,4}"));
            int maali = Integer.parseInt(komentotulkki.findInLine("[0-9]{1,4}"));

            this.haeReitti(alku, maali);
            String polku = komentotulkki.next();
            this.tallennaReitti(polku);

        } else if (komento.substring(0, 5).equals("kaari")) {
            Scanner komentotulkki = new Scanner(komento);
            int alku = Integer.parseInt(komentotulkki.findInLine("[0-9]{1,4}"));
            int maali = Integer.parseInt(komentotulkki.findInLine("[0-9]{1,4}"));

            this.haeKaari(alku, maali);

        } else if (komento.equals("lopeta")) {
            return;
        } else {//from  w  w  w . j a  v  a2s .com
            System.out.println("tuntematon komento");
        }
    }

}

From source file:edu.cmu.lti.oaqa.framework.eval.gs.KeytermGoldStandardFilePersistenceProvider.java

@Override
public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams)
        throws ResourceInitializationException {
    boolean ret = super.initialize(aSpecifier, aAdditionalParams);
    String dataset = (String) getParameterValue("DataSet");
    Pattern lineSyntaxPattern = Pattern.compile((String) getParameterValue("LineSyntax"));
    try {/*from ww w . j a va  2 s  .  com*/
        Resource[] resources = resolver.getResources((String) getParameterValue("PathPattern"));
        for (Resource resource : resources) {
            Scanner scanner = new Scanner(resource.getInputStream());
            while (scanner.findInLine(lineSyntaxPattern) != null) {
                MatchResult result = scanner.match();
                DatasetSequenceId id = new DatasetSequenceId(dataset, result.group(1));
                if (!id2gsSpans.containsKey(id)) {
                    id2gsSpans.put(id, new ArrayList<String>());
                }
                id2gsSpans.get(id).add(result.group(4));
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                } else {
                    break;
                }
            }
            scanner.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}