List of usage examples for java.util.regex Matcher start
public int start()
From source file:Main.java
public static CharSequence handleTextUrl(CharSequence content) { Matcher m = URL_PATTERN.matcher(content); Spannable spannable = null;/*w w w .jav a 2s . c o m*/ while (m.find()) { // Ensure spannable if (spannable == null) { if (content instanceof Spannable) { spannable = (Spannable) content; } else { spannable = new SpannableString(content); } } int start = m.start(); int end = m.end(); URLSpan[] links = spannable.getSpans(start, end, URLSpan.class); if (links.length > 0) { // There has been URLSpan already, leave it alone continue; } URLSpan urlSpan = new URLSpan(m.group(0)); spannable.setSpan(urlSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } return spannable == null ? content : spannable; }
From source file:azkaban.utils.PropsUtils.java
private static String resolveVariableReplacement(String value, Props props, LinkedHashSet<String> visitedVariables) { StringBuffer buffer = new StringBuffer(); int startIndex = 0; Matcher matcher = VARIABLE_REPLACEMENT_PATTERN.matcher(value); while (matcher.find(startIndex)) { if (startIndex < matcher.start()) { // Copy everything up front to the buffer buffer.append(value.substring(startIndex, matcher.start())); }// ww w. j a v a2s. co m String subVariable = matcher.group(1); // Detected a cycle if (visitedVariables.contains(subVariable)) { throw new IllegalArgumentException( String.format("Circular variable substitution found: [%s] -> [%s]", StringUtils.join(visitedVariables, "->"), subVariable)); } else { // Add substitute variable and recurse. String replacement = props.get(subVariable); visitedVariables.add(subVariable); if (replacement == null) { throw new UndefinedPropertyException( String.format("Could not find variable substitution for variable(s) [%s]", StringUtils.join(visitedVariables, "->"))); } buffer.append(resolveVariableReplacement(replacement, props, visitedVariables)); visitedVariables.remove(subVariable); } startIndex = matcher.end(); } if (startIndex < value.length()) { buffer.append(value.substring(startIndex)); } return buffer.toString(); }
From source file:com.reprezen.kaizen.oasparser.jsonoverlay.gen.Template.java
public static <T> String t(String template, T item, String... args) { Matcher matcher = varPat.matcher(template); StringBuffer result = new StringBuffer(); int start = 0; while (matcher.find()) { // would use matcher.appendReplace and matcher.appendTail, but it's way too hard to deal with all the // escaping and quoting. Literal replacement is what I need here. Too bad they don't support that! result.append(template.substring(start, matcher.start())); result.append(replacement(matcher.group(1), item, args)); start = matcher.end();/* ww w . j a va 2 s . c o m*/ } result.append(template.substring(start)); return result.toString(); }
From source file:com.cenrise.test.azkaban.PropsUtils.java
private static String resolveVariableReplacement(final String value, final Props props, final LinkedHashSet<String> visitedVariables) { final StringBuffer buffer = new StringBuffer(); int startIndex = 0; final Matcher matcher = VARIABLE_REPLACEMENT_PATTERN.matcher(value); while (matcher.find(startIndex)) { if (startIndex < matcher.start()) { // Copy everything up front to the buffer buffer.append(value.substring(startIndex, matcher.start())); }/*from w w w.j a va2 s . co m*/ final String subVariable = matcher.group(1); // Detected a cycle if (visitedVariables.contains(subVariable)) { throw new IllegalArgumentException( String.format("Circular variable substitution found: [%s] -> [%s]", StringUtils.join(visitedVariables, "->"), subVariable)); } else { // Add substitute variable and recurse. final String replacement = props.get(subVariable); visitedVariables.add(subVariable); if (replacement == null) { throw new UndefinedPropertyException( String.format("Could not find variable substitution for variable(s) [%s]", StringUtils.join(visitedVariables, "->"))); } buffer.append(resolveVariableReplacement(replacement, props, visitedVariables)); visitedVariables.remove(subVariable); } startIndex = matcher.end(); } if (startIndex < value.length()) { buffer.append(value.substring(startIndex)); } return buffer.toString(); }
From source file:com.googlecode.promnetpp.main.Main.java
private static void preprocessSourceCode() { StringBuilder sourceCodeAsBuilder = new StringBuilder(sourceCode); String commentRegex = "/[*].*?[*]/"; Pattern commentPattern = Pattern.compile(commentRegex, Pattern.DOTALL); Matcher commentMatcher = commentPattern.matcher(sourceCodeAsBuilder); while (commentMatcher.find()) { String comment = commentMatcher.group().replace("/*", "").replace("*/", "").trim(); boolean isAnnotatedComment = comment.startsWith("@"); if (!isAnnotatedComment) { //Remove the comment and reset the matcher sourceCodeAsBuilder.delete(commentMatcher.start(), commentMatcher.end()); commentMatcher = commentPattern.matcher(sourceCodeAsBuilder); }/*from w w w . j a v a 2 s.c om*/ } sourceCode = sourceCodeAsBuilder.toString(); }
From source file:PropertiesHelper.java
/** * Adds new properties to an existing set of properties while * substituting variables. This function will allow value * substitutions based on other property values. Value substitutions * may not be nested. A value substitution will be ${property.key}, * where the dollar-brace and close-brace are being stripped before * looking up the value to replace it with. Note that the ${..} * combination must be escaped from the shell. * * @param a is the initial set of known properties (besides System ones) * @param b is the set of properties to add to a * @return the combined set of properties from a and b. *//*from w w w . j a va 2s .co m*/ protected static Properties addProperties(Properties a, Properties b) { // initial Properties result = new Properties(a); Properties sys = System.getProperties(); Pattern pattern = Pattern.compile("\\$\\{[-a-zA-Z0-9._]+\\}"); for (Enumeration e = b.propertyNames(); e.hasMoreElements();) { String key = (String) e.nextElement(); String value = b.getProperty(key); // unparse value ${prop.key} inside braces Matcher matcher = pattern.matcher(value); StringBuffer sb = new StringBuffer(); while (matcher.find()) { // extract name of properties from braces String newKey = value.substring(matcher.start() + 2, matcher.end() - 1); // try to find a matching value in result properties String newVal = result.getProperty(newKey); // if still not found, try system properties if (newVal == null) { newVal = sys.getProperty(newKey); } // replace braced string with the actual value or empty string matcher.appendReplacement(sb, newVal == null ? "" : newVal); } matcher.appendTail(sb); result.setProperty(key, sb.toString()); } // final return result; }
From source file:com.sun.socialsite.util.UtilitiesModel.java
/** * Transforms the given String into a subset of HTML displayable on a web * page. The subset includes <b>, <i>, <p>, <br>, * <pre> and <a href> (and their corresponding end tags). * * @param s the String to transform//from w w w . j av a2 s .c o m * @return the transformed String */ public static String transformToHTMLSubset(String s) { if (s == null) { return null; } s = replace(s, OPENING_B_TAG_PATTERN, "<b>"); s = replace(s, CLOSING_B_TAG_PATTERN, "</b>"); s = replace(s, OPENING_I_TAG_PATTERN, "<i>"); s = replace(s, CLOSING_I_TAG_PATTERN, "</i>"); s = replace(s, OPENING_BLOCKQUOTE_TAG_PATTERN, "<blockquote>"); s = replace(s, CLOSING_BLOCKQUOTE_TAG_PATTERN, "</blockquote>"); s = replace(s, BR_TAG_PATTERN, "<br />"); s = replace(s, OPENING_P_TAG_PATTERN, "<p>"); s = replace(s, CLOSING_P_TAG_PATTERN, "</p>"); s = replace(s, OPENING_PRE_TAG_PATTERN, "<pre>"); s = replace(s, CLOSING_PRE_TAG_PATTERN, "</pre>"); s = replace(s, OPENING_UL_TAG_PATTERN, "<ul>"); s = replace(s, CLOSING_UL_TAG_PATTERN, "</ul>"); s = replace(s, OPENING_OL_TAG_PATTERN, "<ol>"); s = replace(s, CLOSING_OL_TAG_PATTERN, "</ol>"); s = replace(s, OPENING_LI_TAG_PATTERN, "<li>"); s = replace(s, CLOSING_LI_TAG_PATTERN, "</li>"); s = replace(s, QUOTE_PATTERN, "\""); // HTTP links s = replace(s, CLOSING_A_TAG_PATTERN, "</a>"); Matcher m = OPENING_A_TAG_PATTERN.matcher(s); while (m.find()) { int start = m.start(); int end = m.end(); String link = s.substring(start, end); link = "<" + link.substring(4, link.length() - 4) + ">"; s = s.substring(0, start) + link + s.substring(end, s.length()); m = OPENING_A_TAG_PATTERN.matcher(s); } // escaped angle brackets s = s.replaceAll("&lt;", "<"); s = s.replaceAll("&gt;", ">"); s = s.replaceAll("&#", "&#"); return s; }
From source file:edu.illinois.cs.cogcomp.wikifier.utils.freebase.QueryMQL.java
public static String decodeMQL(String s) { StringBuffer sb = new StringBuffer(); int last = 0; Matcher m = quotedCharPattern.matcher(s); while (m.find()) { int start = m.start(); int end = m.end(); if (start > last) { sb.append(s.substring(last, start)); }// w w w . j av a 2s . com last = end; sb.append((char) Integer.parseInt(s.substring(start + 1, end), 16)); } if (last < s.length()) { sb.append(s.substring(last)); } return sb.toString(); }
From source file:com.sun.socialsite.util.UtilitiesModel.java
/** * Code (stolen from Pebble) to add rel="nofollow" string to all links in HTML. *//*from w w w.j ava 2 s . co m*/ public static String addNofollow(String html) { if (html == null || html.length() == 0) { return html; } Matcher m = mLinkPattern.matcher(html); StringBuffer buf = new StringBuffer(); while (m.find()) { int start = m.start(); int end = m.end(); String link = html.substring(start, end); buf.append(html.substring(0, start)); if (link.indexOf("rel=\"nofollow\"") == -1) { buf.append(link.substring(0, link.length() - 1) + " rel=\"nofollow\">"); } else { buf.append(link); } html = html.substring(end, html.length()); m = mLinkPattern.matcher(html); } buf.append(html); return buf.toString(); }
From source file:org.yamj.core.service.mediaimport.FilenameScanner.java
private static String cutMatch(String rest, Matcher matcher) { return (rest.substring(0, matcher.start()) + rest.substring(matcher.end())).trim(); }