List of usage examples for java.util.regex Matcher start
public int start()
From source file:be.makercafe.apps.makerbench.editors.XMLEditor.java
private StyleSpans<Collection<String>> computeHighlighting(String text) { Matcher matcher = XML_TAG.matcher(text); int lastKwEnd = 0; StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>(); while (matcher.find()) { spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd); if (matcher.group("COMMENT") != null) { spansBuilder.add(Collections.singleton("comment"), matcher.end() - matcher.start()); } else {/*from w w w . j a v a 2s . c om*/ if (matcher.group("ELEMENT") != null) { String attributesText = matcher.group(GROUP_ATTRIBUTES_SECTION); spansBuilder.add(Collections.singleton("tagmark"), matcher.end(GROUP_OPEN_BRACKET) - matcher.start(GROUP_OPEN_BRACKET)); spansBuilder.add(Collections.singleton("anytag"), matcher.end(GROUP_ELEMENT_NAME) - matcher.end(GROUP_OPEN_BRACKET)); if (!attributesText.isEmpty()) { lastKwEnd = 0; Matcher amatcher = ATTRIBUTES.matcher(attributesText); while (amatcher.find()) { spansBuilder.add(Collections.emptyList(), amatcher.start() - lastKwEnd); spansBuilder.add(Collections.singleton("attribute"), amatcher.end(GROUP_ATTRIBUTE_NAME) - amatcher.start(GROUP_ATTRIBUTE_NAME)); spansBuilder.add(Collections.singleton("tagmark"), amatcher.end(GROUP_EQUAL_SYMBOL) - amatcher.end(GROUP_ATTRIBUTE_NAME)); spansBuilder.add(Collections.singleton("avalue"), amatcher.end(GROUP_ATTRIBUTE_VALUE) - amatcher.end(GROUP_EQUAL_SYMBOL)); lastKwEnd = amatcher.end(); } if (attributesText.length() > lastKwEnd) spansBuilder.add(Collections.emptyList(), attributesText.length() - lastKwEnd); } lastKwEnd = matcher.end(GROUP_ATTRIBUTES_SECTION); spansBuilder.add(Collections.singleton("tagmark"), matcher.end(GROUP_CLOSE_BRACKET) - lastKwEnd); } } lastKwEnd = matcher.end(); } spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd); return spansBuilder.create(); }
From source file:at.ac.tuwien.inso.subcat.utility.commentparser.Parser.java
License:asdf
private void parseQuotes(List<ContentNode<T>> ast, String comment) { Matcher m = pQuote.matcher(comment); int lastEnd = 0; while (m.find()) { if (lastEnd >= 0 && lastEnd != m.start()) { parseParagraphs(ast, comment.substring(lastEnd, m.start())); }/*from ww w. j a v a 2 s . com*/ String content = m.group(0); String idStr = m.group(2); int id = (idStr != null) ? Integer.parseInt(idStr) : -1; Matcher cfm = pCleanFstQuote.matcher(content); content = cfm.replaceAll(""); Matcher csm = pCleanSubQuote.matcher(content); content = csm.replaceAll(""); String[] contentFragments = pQuoteSplitter.split(content.trim()); String[] cleanedFragments = new String[contentFragments.length]; for (int i = 0; i < contentFragments.length; i++) { Matcher normM = pNorm.matcher(contentFragments[i]); cleanedFragments[i] = normM.replaceAll(" "); } ast.add(new QuoteNode<T>(cleanedFragments, id)); lastEnd = m.end(); } if (lastEnd != comment.length()) { String frag = comment.substring(lastEnd, comment.length()); if (frag.trim().length() > 0) { parseGLibMessages(ast, frag); } } }
From source file:com.qmetry.qaf.automation.step.StringTestStep.java
private void absractArgsAandSetDesciption() { String prefix = BDDKeyword.getKeywordFrom(name); if (actualArgs == null || actualArgs.length == 0) { final String REGEX = ParamType.getParamValueRegx(); List<String> arguments = new ArrayList<String>(); description = removePrefix(prefix, name); Pattern p = Pattern.compile(REGEX); Matcher m = p.matcher(description); // get a matcher object int count = 0; while (m.find()) { String arg = description.substring(m.start(), m.end()); arguments.add(arg);//from w w w . j a va 2 s.c o m count++; } for (int i = 0; i < count; i++) { description = description.replaceFirst(Pattern.quote(arguments.get(i)), Matcher.quoteReplacement("{" + i + "}")); } actualArgs = arguments.toArray(new String[] {}); } name = StringUtil.toCamelCaseIdentifier(description.length() > 0 ? description : name); }
From source file:de.ks.text.AsciiDocParser.java
private String copyFiles(String parse, File dataDir) throws IOException { Pattern pattern = Pattern.compile("\"file:.*\""); Matcher matcher = pattern.matcher(parse); int bodyTag = parse.indexOf("<body"); Map<String, String> replacements = new HashMap<>(); while (matcher.find()) { int start = matcher.start(); if (start < bodyTag) { continue; }//from w w w.j a v a 2s .c om int end = matcher.end(); String fileReference = parse.substring(start + 1, end - 1); end = fileReference.indexOf("\""); fileReference = fileReference.substring(0, end); log.debug("Found file reference {}", fileReference); URI uri = URI.create(fileReference.replace('\\', '/')); File sourceFile = new File(uri); File targetFile = new File(dataDir, sourceFile.getName()); java.nio.file.Files.copy(sourceFile.toPath(), targetFile.toPath()); replacements.put(fileReference, dataDir.getName() + "/" + targetFile.getName()); } for (Map.Entry<String, String> entry : replacements.entrySet()) { String original = entry.getKey(); String replacement = entry.getValue(); parse = StringUtils.replace(parse, original, replacement); } return parse; }
From source file:eu.eexcess.sourceselection.redde.indexer.trec.topic.TrecTopicTokenizer.java
/** * Tokenize trec-topic xml file respecting missing closing xml tags. * @param input/* ww w . ja v a 2 s . c o m*/ * @return * @throws IOException */ public List<Topic> tokenize(InputStream input) throws IOException { Writer writer = new StringWriter(); IOUtils.copy(input, writer); String data = writer.toString(); Matcher startTopicMatcher = getStartTopicTagMatcher(data); Matcher endTopicTagMatcher = getEndTopicTagMatcher(data); int processedChars = 0; while (startTopicMatcher.find(processedChars)) { if (endTopicTagMatcher.find(startTopicMatcher.end())) { String topicData = data.substring(processedChars, endTopicTagMatcher.start()); Matcher startTagMatcher = getStartGenericTagMatcher(topicData); while (startTagMatcher.find()) { Matcher endTagMatcher = getStartGenericTagMatcher(topicData); if (endTagMatcher.find(startTagMatcher.end())) { assignValueToField(startTagMatcher.group(), topicData.substring(startTagMatcher.end(), endTagMatcher.start())); } else { // reached last tag in topic assignValueToField(startTagMatcher.group(), topicData.substring(startTagMatcher.end(), topicData.length())); } } } else { throw new IllegalArgumentException("expected topic end tag"); } processedChars = endTopicTagMatcher.end(); topics.add(currentTopic); currentTopic = new Topic(); } return topics; }
From source file:me.timothy.twittertoreddit.TwitterToReddit.java
public void postTweet(Status status) { if (status.getUser().getId() != twitterId) return;/*from w w w . j a va 2 s .com*/ String text = status.getText(); // Find the first hyperlink System.out.println(status.getUser().getName() + " - " + text); Matcher matcher = URL_PATTERN.matcher(text); if (matcher.find()) { String firstLink = matcher.group(); System.out.println(" Link: " + firstLink); firstLink = LinkUnshortener.unshorten(restClient, firstLink); System.out.println(" Unshortened: " + firstLink); String realText = text.substring(0, matcher.start()).trim(); try { redditUser.submitLink(realText, firstLink, subreddit); } catch (IOException | ParseException e) { e.printStackTrace(); System.exit(1); } } else { System.out.println(" Failed to find link"); } }
From source file:com.googlecode.osde.internal.builders.GadgetBuilder.java
private void compileGadgetSpec(IFile source, IFile target, IProject project, IProgressMonitor monitor) throws CoreException, ParserException, UnsupportedEncodingException { Parser<Module> parser = ParserFactory.gadgetSpecParser(); InputStream fileContent = source.getContents(); Module module;// w ww . j a v a 2 s . c o m try { module = parser.parse(fileContent); } finally { IOUtils.closeQuietly(fileContent); } List<Content> contents = module.getContent(); Random rnd = new Random(); for (Content content : contents) { if (ViewType.html.toString().equals(content.getType())) { String value = content.getValue(); Pattern pattern = Pattern.compile("http://localhost:[0-9]+/" + project.getName() + "/[-_.!~*\\'()a-zA-Z0-9;\\/?:\\@&=+\\$,%#]+\\.js"); Matcher matcher = pattern.matcher(value); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, value.substring(matcher.start(), matcher.end()) + "?rnd=" + Math.abs(rnd.nextInt())); } matcher.appendTail(sb); content.setValue(sb.toString()); } } String serialized = GadgetXmlSerializer.serialize(module); ByteArrayInputStream content = new ByteArrayInputStream(serialized.getBytes("UTF-8")); target.create(content, IResource.DERIVED | IResource.FORCE, monitor); }
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/*from ww w . ja v a 2 s . 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:com.apigee.sdk.apm.http.impl.client.cache.WarningValue.java
protected void consumeHostPort() { Matcher m = HOSTPORT_PATTERN.matcher(src.substring(offs)); if (!m.find()) parseError();/* w ww. ja v a 2 s . c o m*/ if (m.start() != 0) parseError(); offs += m.end(); }
From source file:com.javielinux.utils.Utils.java
static public String toHTML(Context context, String text, String underline) { 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 ww .ja va2 s. c o m*/ 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()); } ThemeManager theme = new ThemeManager(context); String tweet_color_link = theme.getStringColor("tweet_color_link"); String tweet_color_hashtag = theme.getStringColor("tweet_color_hashtag"); String tweet_color_user = theme.getStringColor("tweet_color_user"); 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 (link.equals(underline)) { link = "<u>" + out.substring(s, e) + "</u>"; } if (out.substring(s, s + 1).equals("#")) { out = out.substring(0, s) + "<font color=\"#" + tweet_color_hashtag + "\">" + link + "</font>" + out.substring(e, out.length()); } else if (out.substring(s, s + 1).equals("@")) { out = out.substring(0, s) + "<font color=\"#" + tweet_color_user + "\">" + link + "</font>" + out.substring(e, out.length()); } else { out = out.substring(0, s) + "<font color=\"#" + tweet_color_link + "\">" + link + "</font>" + out.substring(e, out.length()); } } return out; }