List of usage examples for java.util.regex Matcher end
public int end()
From source file:com.apigee.sdk.apm.http.impl.client.cache.WarningValue.java
protected void consumeWarnDate() { int curr = offs; Matcher m = WARN_DATE_PATTERN.matcher(src.substring(offs)); if (!m.lookingAt()) parseError();/*from w w w. j a v a 2 s . c o m*/ offs += m.end(); try { warnDate = DateUtils.parseDate(src.substring(curr + 1, offs - 1)); } catch (DateParseException e) { throw new IllegalStateException("couldn't parse a parseable date"); } }
From source file:com.norconex.importer.handler.transformer.impl.StripBetweenTransformer.java
@Override protected void transformStringContent(String reference, StringBuilder content, ImporterMetadata metadata, boolean parsed, boolean partialContent) { int flags = Pattern.DOTALL | Pattern.UNICODE_CASE; if (!caseSensitive) { flags = flags | Pattern.CASE_INSENSITIVE; }//from w w w.j a v a 2 s .c o m for (Pair<String, String> pair : stripPairs) { List<Pair<Integer, Integer>> matches = new ArrayList<Pair<Integer, Integer>>(); Pattern leftPattern = Pattern.compile(pair.getLeft(), flags); Matcher leftMatch = leftPattern.matcher(content); while (leftMatch.find()) { Pattern rightPattern = Pattern.compile(pair.getRight(), flags); Matcher rightMatch = rightPattern.matcher(content); if (rightMatch.find(leftMatch.end())) { if (inclusive) { matches.add(new ImmutablePair<Integer, Integer>(leftMatch.start(), rightMatch.end())); } else { matches.add(new ImmutablePair<Integer, Integer>(leftMatch.end(), rightMatch.start())); } } else { break; } } for (int i = matches.size() - 1; i >= 0; i--) { Pair<Integer, Integer> matchPair = matches.get(i); content.delete(matchPair.getLeft(), matchPair.getRight()); } } }
From source file:com.haulmont.cuba.core.global.QueryTransformerRegex.java
@Override public void addJoinAsIs(String join) { Matcher matcher = findReturnedEntityDeclaration(); int insertPos = matcher.end(); buffer.insert(insertPos, " "); insertPos++;/*from w w w . j ava 2 s . co m*/ buffer.insert(insertPos, join); Matcher paramMatcher = PARAM_PATTERN.matcher(join); while (paramMatcher.find()) { addedParams.add(paramMatcher.group(1)); } }
From source file:net.sf.jabref.importer.fetcher.JSTORFetcher2.java
private String getCitationsFromUrl(String urlQuery, List<String> ids, int count, String[] numberOfRefs, ImportInspector dialog, OutputPrinter status) throws IOException { URL url = new URL(urlQuery); URLDownload ud = new URLDownload(url); String cont = ud.downloadToString(); String entirePage = cont;/* ww w . j a va2 s . com*/ String pageEntire = cont; int countOfRefs = 0; int refsRequested; if (count == 1) { // Readin the numberofhits (only once) Matcher mn = JSTORFetcher2.numberofhits.matcher(pageEntire); if (mn.find()) { //System.out.println("JSTORFetcher2 getCitationsFromUrl numberofhits=" + mn.group(1)); numberOfRefs[0] = mn.group(1); countOfRefs = Integer.valueOf(numberOfRefs[0]); //System.out.println("JSTORFetcher2 getCitationsFromUrl numberofrefs[0]=" + Integer.valueOf(numberOfRefs[0])); } else { //System.out.println("JSTORFetcher2 getCitationsFromUrl cant find numberofhits="); numberOfRefs[0] = "0"; } while (true) { String strCount = JOptionPane.showInputDialog( Localization.lang("References found") + ": " + countOfRefs + " " + Localization.lang("Number of references to fetch?"), Integer.toString(countOfRefs)); if (strCount == null) { status.setStatus(Localization.lang("JSTOR import cancelled")); return JSTORFetcher2.CANCELLED; } try { numberOfRefs[1] = strCount.trim(); refsRequested = Integer.parseInt(numberOfRefs[1]); break; } catch (RuntimeException ex) { status.showMessage(Localization.lang("Please enter a valid number")); } } } countOfRefs = Integer.valueOf(numberOfRefs[0]); refsRequested = Integer.valueOf(numberOfRefs[1]); Matcher m = JSTORFetcher2.idPattern.matcher(cont); if (m.find() && ((ids.size() + 1) <= refsRequested)) { do { ids.add(m.group(1)); cont = cont.substring(m.end()); m = JSTORFetcher2.idPattern.matcher(cont); } while (m.find() && ((ids.size() + 1) <= refsRequested)); } else if (entirePage.contains(JSTORFetcher2.noAccessIndicator)) { noAccessFound = true; return null; } else { return null; } m = JSTORFetcher2.nextPagePattern.matcher(entirePage); if (m.find()) { return JSTORFetcher2.JSTOR_URL + m.group(1); } else { return null; } }
From source file:io.kahu.hawaii.util.call.http.util.UriBuilder.java
private void substitutePathVariables(StringBuilder builder, String url) { Matcher matcher = pathVariablePattern.matcher(url); int currentPos = 0; int matchNumber = 0; while (matcher.find()) { int matchStart = matcher.start(); if (matchStart > currentPos) { builder.append(url.substring(currentPos, matchStart)); }/* w w w .j a va 2 s . c o m*/ builder.append(pathVariables[matchNumber]); currentPos = matcher.end(); matchNumber++; } if (currentPos < url.length()) { builder.append(url.substring(currentPos)); } if (matchNumber != pathVariables.length) { System.err.println("Substitution for '" + url + "' got '" + pathVariables.length + "' variables and the number of groups is '" + matcher.groupCount() + "'."); } }
From source file:com.sencha.gxt.core.rebind.XTemplateParser.java
public TemplateModel parse(String template) throws UnableToCompleteException { // look for parameters or tags (Consider combining into one pattern) TemplateModel model = new TemplateModel(); Stack<ContainerTemplateChunk> stack = new Stack<ContainerTemplateChunk>(); stack.push(model);//ww w . ja v a2 s. c o m Matcher m = NON_LITERAL_PATTERN.matcher(template); int lastMatchEnd = 0; while (m.find()) { // range of the current non-literal int begin = m.start(), end = m.end(); String currentMatch = template.substring(begin, end); // if there was content since the last non-literal chunk, track it if (lastMatchEnd < begin) { ContentChunk c = literal(template.substring(lastMatchEnd, begin)); stack.peek().children.add(c); log(c); } // move the last match pointer lastMatchEnd = end; // tpl tag starting Matcher tagOpenMatch = TAG_PATTERN.matcher(currentMatch); if (tagOpenMatch.matches()) { ControlChunk c = new ControlChunk(); c.controls = new HashMap<String, String>(); String attrs = tagOpenMatch.group(1).trim(); Matcher attrMatcher = ATTR_PATTERN.matcher(attrs); while (attrMatcher.find()) { // should be if or for String key = attrMatcher.group(1); // must be html-decoded String encodedValue = attrMatcher.group(2) == null ? attrMatcher.group(3) : attrMatcher.group(2); String value = StringEscapeUtils.unescapeXml(encodedValue); c.controls.put(key, value); } stack.peek().children.add(c); stack.push(c); log(c); continue; } // tpl tag ending Matcher tagCloseMatch = TAG_CLOSE_PATTERN.matcher(currentMatch); if (tagCloseMatch.matches()) { TemplateChunk c; try { c = stack.pop(); } catch (EmptyStackException ex) { logger.log(Type.ERROR, "Too many </tpl> tags"); throw new UnableToCompleteException(); } log(c); continue; } // reference (code) Matcher codeMatch = INVOKE_PATTERN.matcher(currentMatch); if (codeMatch.matches()) { ContentChunk c = new ContentChunk(); c.type = ContentType.CODE; c.content = codeMatch.group(1); stack.peek().children.add(c); log(c); continue; } // reference (param) Matcher paramMatch = PARAM_PATTERN.matcher(currentMatch); if (paramMatch.matches()) { ContentChunk c = new ContentChunk(); c.type = ContentType.REFERENCE; c.content = paramMatch.group(1); stack.peek().children.add(c); log(c); continue; } } // handle trailing content if (lastMatchEnd < template.length()) { ContentChunk c = literal(template.substring(lastMatchEnd)); log(c); model.children.add(c); } if (model != stack.peek()) { logger.log(Type.ERROR, "Too few </tpl> tags"); throw new UnableToCompleteException(); } return model; }
From source file:net.sf.jabref.openoffice.BstWrapper.java
private Map<String, String> parseResult(String result) { Map<String, String> map = new HashMap<>(); // Look through for instances of \bibitem : Matcher m = BstWrapper.bibitemTag.matcher(result); ArrayList<Integer> indices = new ArrayList<>(); ArrayList<Integer> endIndices = new ArrayList<>(); ArrayList<String> keys = new ArrayList<>(); while (m.find()) { if (!indices.isEmpty()) { endIndices.add(m.start());/*from w ww . java 2 s . co m*/ } LOGGER.debug(m.start() + " " + m.end()); String tag = m.group(); String key = tag.substring(9, tag.length() - 1); indices.add(m.end()); keys.add(key); } int lastI = result.lastIndexOf("\\end{thebibliography}"); if ((lastI > 0) && (lastI > indices.get(indices.size() - 1))) { endIndices.add(lastI); } for (int i = 0; i < keys.size(); i++) { String key = keys.get(i); int index = indices.get(i); int endIndex = endIndices.get(i); String part = result.substring(index, endIndex); map.put(key, formatter.format(part.trim().replaceAll("\\\\newblock ", " "))); } return map; }
From source file:com.javielinux.utils.Utils.java
static public String toExportHTML(Context context, String text) { String out = text.replace("<", "<"); //out = out.replace(">", ">"); ArrayList<Integer> valStart = new ArrayList<Integer>(); ArrayList<Integer> valEnd = new ArrayList<Integer>(); Comparator<Integer> comparator = Collections.reverseOrder(); // enlaces/*w w w. ja v a2s. c om*/ String regex = "\\(?\\b(http://|https://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(out); while (m.find()) { valStart.add(m.start()); valEnd.add(m.end()); } // hashtag regex = "(#[\\w-]+)"; p = Pattern.compile(regex); m = p.matcher(out); while (m.find()) { valStart.add(m.start()); valEnd.add(m.end()); } // usuarios twitter regex = "(@[\\w-]+)"; p = Pattern.compile(regex); m = p.matcher(out); while (m.find()) { valStart.add(m.start()); valEnd.add(m.end()); } Collections.sort(valStart, comparator); Collections.sort(valEnd, comparator); for (int i = 0; i < valStart.size(); i++) { int s = valStart.get(i); int e = valEnd.get(i); String link = out.substring(s, e); if (out.substring(s, s + 1).equals("#")) { out = out.substring(0, s) + "<a href=\"http://twitter.com/#!/search/" + link + "\" class=\"hashtag\">" + link + "</a>" + out.substring(e, out.length()); } else if (out.substring(s, s + 1).equals("@")) { out = out.substring(0, s) + "<a href=\"http://twitter.com/#!/" + link + "\" class=\"user\">" + link + "</a>" + out.substring(e, out.length()); } else { out = out.substring(0, s) + "<a href=\"" + link + "\" class=\"link\">" + link + "</a>" + out.substring(e, out.length()); } } return out; }
From source file:be.makercafe.apps.makerbench.editors.JFXScadEditor.java
private static StyleSpans<Collection<String>> computeHighlighting(String text) { Matcher matcher = PATTERN.matcher(text); int lastKwEnd = 0; StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>(); while (matcher.find()) { String styleClass = matcher.group("KEYWORD") != null ? "keyword" : matcher.group("PAREN") != null ? "paren" : matcher.group("BRACE") != null ? "brace" : matcher.group("BRACKET") != null ? "bracket" : matcher.group("SEMICOLON") != null ? "semicolon" : matcher.group("STRING") != null ? "string" : matcher.group("COMMENT") != null ? "comment" : null; /* never happens */ assert styleClass != null; spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd); spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start()); lastKwEnd = matcher.end();//w w w .jav a 2 s. c o m } spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd); return spansBuilder.create(); }
From source file:com.norconex.importer.handler.tagger.impl.TextBetweenTagger.java
@Override protected void tagStringContent(String reference, StringBuilder content, ImporterMetadata metadata, boolean parsed, boolean partialContent) { int flags = Pattern.DOTALL | Pattern.UNICODE_CASE; if (!caseSensitive) { flags = flags | Pattern.CASE_INSENSITIVE; }/*from w w w. j a v a 2 s . co m*/ for (TextBetween between : betweens) { List<Pair<Integer, Integer>> matches = new ArrayList<Pair<Integer, Integer>>(); Pattern leftPattern = Pattern.compile(between.start, flags); Matcher leftMatch = leftPattern.matcher(content); while (leftMatch.find()) { Pattern rightPattern = Pattern.compile(between.end, flags); Matcher rightMatch = rightPattern.matcher(content); if (rightMatch.find(leftMatch.end())) { if (inclusive) { matches.add(new ImmutablePair<Integer, Integer>(leftMatch.start(), rightMatch.end())); } else { matches.add(new ImmutablePair<Integer, Integer>(leftMatch.end(), rightMatch.start())); } } else { break; } } for (int i = matches.size() - 1; i >= 0; i--) { Pair<Integer, Integer> matchPair = matches.get(i); String value = content.substring(matchPair.getLeft(), matchPair.getRight()); if (value != null) { metadata.addString(between.name, value); } } } }