List of usage examples for java.util Scanner match
public MatchResult match()
From source file:Main.java
public static void main(String[] args) { String s = "java2s.com 1 + 1 = 2.0 "; Scanner scanner = new Scanner(s); // check if next token is "java" System.out.println(scanner.hasNext("java")); // find the last match and print it System.out.println(scanner.match()); System.out.println(scanner.nextLine()); scanner.close();/*from w w w.j av a 2 s .c om*/ }
From source file:Main.java
public static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws Exception { InputStream in = null;/* w w w. j ava 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)); }//w w w. j a v a 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); }// ww w . j a v a2 s.co m return names; }
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)); }/*www .j a va2 s . c om*/ 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 w w . ja v a 2s .c om MatchResult result = s.match(); assertThat(result.groupCount()).isEqualTo(2); assertThat(result.group(1)).isEqualTo("28"); assertThat(result.group(2)).isEqualTo("47"); }
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 w w .j av a 2 s .c om 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; }
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. c o m*/ 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; }
From source file:com.jaxio.celerio.template.VelocityGenerator.java
private void displayLinesInError(VelocityException exception, TemplatePack templatePack, Template template) { try {// www . ja v a 2 s .co m 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:edu.cmu.lti.oaqa.framework.eval.gs.PassageGoldStandardFilePersistenceProvider.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 w w w . j a v a 2 s .c o m*/ 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)); List<GoldStandardSpan> list = id2gsSpans.get(id); if (list == null) { list = new ArrayList<GoldStandardSpan>(); id2gsSpans.put(id, list); } GoldStandardSpan annotation = new GoldStandardSpan(result.group(2), Integer.parseInt(result.group(3)), Integer.parseInt(result.group(4)), result.group(5)); list.add(annotation); if (scanner.hasNextLine()) { scanner.nextLine(); } else { break; } } scanner.close(); } } catch (IOException e) { e.printStackTrace(); } return ret; }