List of usage examples for java.util.regex Matcher appendReplacement
public Matcher appendReplacement(StringBuilder sb, String replacement)
From source file:org.etudes.util.HtmlHelper.java
/** * Remove any text that match the "comments from Word style definitions encoded into html by Tiny" from the data. * //from www . j a va 2s .c o m * @param data * the html data. * @return The cleaned up data. */ public static String stripEncodedStyleDefinitionComments(String data) { if (data == null) return data; // quick check for any hint of the pattern if (data.indexOf("<!-- /* Style Definitions */") == -1) return data; // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost Pattern p = Pattern.compile("<!-- /\\* Style Definitions \\*/.*?-->", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL); Matcher m = p.matcher(data); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, ""); } m.appendTail(sb); return sb.toString(); }
From source file:org.etudes.util.HtmlHelper.java
/** * Remove any text that match the "comments damaged from IE and Tiny" from the data. * /*from w w w . j av a 2s . c o m*/ * @param data * the html data. * @return The cleaned up data. */ public static String stripDamagedComments(String data) { if (data == null) return data; // quick check for any hint of the pattern if (data.indexOf("<! [endif] >") == -1) return data; // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost Pattern p = Pattern.compile("<!--\\[if.*?<! \\[endif\\] >", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL); Matcher m = p.matcher(data); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, ""); } m.appendTail(sb); // now remove the bad comment end String rv = sb.toString().replace("<-->", ""); return rv; }
From source file:org.etudes.util.HtmlHelper.java
/** * Remove any HTML comments from the data. * /* www .ja v a 2 s . c o m*/ * @param data * the html data. * @return The cleaned up data. */ public static String stripComments(String data) { if (data == null) return data; // quick check for any comments if (data.indexOf("<!--") == -1) return data; // pattern to find html comments // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost Pattern p = Pattern.compile("<!--.*?-->", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL); Matcher m = p.matcher(data); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, ""); } m.appendTail(sb); data = sb.toString(); // if any open tags are left, likely because of missing a matching close tag, we will remove them. // if we leave them in, a missing close comment tag will be inserted by HtmlCleaner at the very END of the document, making the rest a big comment. // this fix exposes some comment text into the content, but preserves actual content. data = data.replaceAll("<!--", ""); return data; }
From source file:Main.java
public static String escape(String message) { if (message == null) return message; Matcher matcher = escapePattern.matcher(message); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String replacement;//from w ww .jav a 2s . c o m if ("\"".equals(matcher.group())) replacement = """; else if ("&".equals(matcher.group())) replacement = "&"; else if ("'".equals(matcher.group())) replacement = "'"; else if ("<".equals(matcher.group())) replacement = "<"; else if (">".equals(matcher.group())) replacement = ">"; else throw new IllegalArgumentException(); matcher.appendReplacement(sb, replacement); } matcher.appendTail(sb); return sb.toString(); }
From source file:nl.armatiek.xslweb.utils.XSLWebUtils.java
public static String resolveProperties(String sourceString, Properties props) { if (sourceString == null) { return null; }//from w w w . j a v a2 s. co m Matcher m = variablesPattern.matcher(sourceString); StringBuffer result = new StringBuffer(); while (m.find()) { String variable = m.group(1); String value = props.getProperty(variable); if (value == null) { throw new XSLWebException(String.format("No value specified for variable \"%s\"", variable)); } String resolved = resolveProperties(value.toString(), props); resolved = resolved.replaceAll("([\\\\\\$])", "\\\\$1"); m.appendReplacement(result, resolved); } m.appendTail(result); return result.toString(); }
From source file:com.google.api.tools.framework.aspects.documentation.model.DocumentationUtil.java
/** * Given a string, searches for unqualified references to message fields and to method names and * converts them from RPC-style to REST-style. Field names are converted from lower_underscore to * lowerCamel. Method names are converted from VerbCollection-style to collection.verb style. * No work is done for qualified references; in such a case, an explicit markdown link with the * proper display text should be used in the proto comment (e.g., * [foo_bar][Path.To.Message.foo_bar], which will be converted to * [fooBar][Path.To.Message.foo_bar] by rpcToRest). *///ww w . j av a 2 s.co m public static String rpcToRest(SymbolTable symbolTable, String description) { StringBuffer sb = new StringBuffer(); Matcher m = WORD.matcher(description); while (m.find()) { // Convert field names if (symbolTable.containsFieldName(m.group(0))) { m.appendReplacement(sb, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, m.group(0))); // Convert method names } else if (symbolTable.lookupMethodSimpleName(m.group(0)) != null) { RestMethod restMethod = RestMethod .getPrimaryRestMethod(symbolTable.lookupMethodSimpleName(m.group(0)).get(0)); if (restMethod == null) { m.appendReplacement(sb, m.group(0)); } else { m.appendReplacement(sb, restMethod.getSimpleRestCollectionName() + "." + restMethod.getRestMethodName()); } } else { m.appendReplacement(sb, m.group(0)); } } m.appendTail(sb); return sb.toString(); }
From source file:Inflector.java
/** * Utility method to replace all occurrences given by the specific backreference with its uppercased form, and remove all * other backreferences.//from ww w . j a v a2 s . co m * <p> * The Java {@link Pattern regular expression processing} does not use the preprocessing directives <code>\l</code>, * <code>\u</code>, <code>\L</code>, and <code>\U</code>. If so, such directives could be used in the replacement string * to uppercase or lowercase the backreferences. For example, <code>\L1</code> would lowercase the first backreference, and * <code>\u3</code> would uppercase the 3rd backreference. * </p> * * @param input * @param regex * @param groupNumberToUppercase * @return the input string with the appropriate characters converted to upper-case */ protected static String replaceAllWithUppercase(String input, String regex, int groupNumberToUppercase) { Pattern underscoreAndDotPattern = Pattern.compile(regex); Matcher matcher = underscoreAndDotPattern.matcher(input); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, matcher.group(groupNumberToUppercase).toUpperCase()); } matcher.appendTail(sb); return sb.toString(); }
From source file:ac.elements.parser.ExtendedFunctions.java
/** * Within a specified input string, replaces all strings that match a * regular expression pattern with a specified replacement string. * // w w w . j a v a2s . c om * @param input * the input string (eg "aabfooaabfooabfoob") * @param regex * the regular expression (eg "a*b") * @param replace * the replacement string (eg "-") * * @return the resulting string (eg "-foo-foo-foo-") */ public static String regex(String input, String regex, String replace) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, replace); } return sb.toString(); }
From source file:org.apache.sling.contextaware.config.impl.metadata.AnnotationClassParser.java
/** * Implements the method name mapping as defined in OSGi R6 Compendium specification, * Chapter 112. Declarative Services Specification, Chapter 112.8.2.1. Component Property Mapping. * @param methodName Method name/*from ww w . ja v a 2 s. c o m*/ * @return Mapped property name */ public static String getPropertyName(String methodName) { Matcher matcher = METHOD_NAME_MAPPING.matcher(methodName); StringBuffer mappedName = new StringBuffer(); while (matcher.find()) { String replacement = ""; if (matcher.group(1) != null) { replacement = "\\$"; } if (matcher.group(2) != null) { replacement = ""; } if (matcher.group(3) != null) { replacement = "_"; } if (matcher.group(4) != null) { replacement = "."; } matcher.appendReplacement(mappedName, replacement); } matcher.appendTail(mappedName); return mappedName.toString(); }
From source file:org.eclipse.scada.utils.str.StringReplacer.java
/** * Replace variables in a string//from w w w . j ava 2 s .c o m * * @param string * the string to process * @param replaceSource * the source of the replacements * @param pattern * the pattern for detecting variables * @param nested * <code>true</code> if the replacement process should honor * nested replacements * @return the replaced string, or <code>null</code> if the input string was * <code>null</code> */ public static String replace(final String string, final ReplaceSource replaceSource, final Pattern pattern, final boolean nested) { if (string == null) { return null; } final Matcher m = pattern.matcher(string); boolean result = m.find(); if (result) { final StringBuffer sb = new StringBuffer(); do { final String key; if (m.groupCount() > 0) { key = m.group(1); } else { key = null; } String replacement = replaceSource.replace(m.group(0), key); if (replacement == null) { m.appendReplacement(sb, ""); } else { replacement = replacement.replace("\\", "\\\\").replace("$", "\\$"); m.appendReplacement(sb, replacement); } result = m.find(); } while (result); m.appendTail(sb); final String resultString = sb.toString(); if (nested && !string.equals(resultString)) { /* * only try again if we should handle nested replacements and if something changed */ return replace(resultString, replaceSource, pattern, true); } else { return resultString; } } else { return string; } }