List of usage examples for java.util.regex Matcher appendTail
public StringBuilder appendTail(StringBuilder sb)
From source file:com.epam.reportportal.jbehave.JBehaveUtils.java
@VisibleForTesting static String expandParameters(String stepName, Map<String, String> parameters) { Matcher m = STEP_NAME_PATTERN.matcher(stepName); StringBuffer buffer = new StringBuffer(); while (m.find()) { if (parameters.containsKey(m.group(1))) { m.appendReplacement(buffer, parameters.get(m.group(1))); }//from w w w . ja v a2 s . co m } m.appendTail(buffer); return buffer.toString(); }
From source file:org.apache.jmeter.report.dashboard.ReportGenerator.java
/** * <p>//from w w w . j a v a 2s . c o m * Gets the name of property setter from the specified key. * </p> * <p> * E.g : with key set_granularity, returns setGranularity (camel case) * </p> * * @param propertyKey * the property key * @return the name of the property setter */ private static String getSetterName(String propertyKey) { Matcher matcher = POTENTIAL_CAMEL_CASE_PATTERN.matcher(propertyKey); StringBuffer buffer = new StringBuffer(); // Unfortunately Matcher does not support StringBuilder while (matcher.find()) { matcher.appendReplacement(buffer, matcher.group(1).toUpperCase()); } matcher.appendTail(buffer); return buffer.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 ww. ja v a2s. 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: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 www . j a v a 2 s . com*/ 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:gsn.storage.SQLUtils.java
public static StringBuilder newRewrite(CharSequence query, CharSequence tableNameToRename, CharSequence replaceTo) { // Selecting strings between pair of "" : (\"[^\"]*\") // Selecting tableID.tableName or tableID.* : (\\w+(\\.(\w+)|\\*)) // The combined pattern is : (\"[^\"]*\")|(\\w+\\.((\\w+)|\\*)) Matcher matcher = pattern.matcher(query); StringBuffer result = new StringBuffer(); while (matcher.find()) { if (matcher.group(2) == null) continue; String tableName = matcher.group(3); if (tableName.equals(tableNameToRename)) { // $4 means that the 4th group of the match should be appended to the // string (the forth group contains the field name). if (replaceTo != null) matcher.appendReplacement(result, new StringBuilder(replaceTo).append("$4").toString()); }//w ww . ja va 2 s. co m } String toReturn = matcher.appendTail(result).toString().toLowerCase(); int indexOfFrom = toReturn.indexOf(" from ") >= 0 ? toReturn.indexOf(" from ") + " from ".length() : 0; int indexOfWhere = (toReturn.lastIndexOf(" where ") > 0 ? (toReturn.lastIndexOf(" where ")) : toReturn.length()); String selection = toReturn.substring(indexOfFrom, indexOfWhere); Pattern fromClausePattern = Pattern.compile("\\s*(\\w+)\\s*", Pattern.CASE_INSENSITIVE); Matcher fromClauseMather = fromClausePattern.matcher(selection); result = new StringBuffer(); while (fromClauseMather.find()) { if (fromClauseMather.group(1) == null) continue; String tableName = fromClauseMather.group(1); if (tableName.equals(tableNameToRename) && replaceTo != null) fromClauseMather.appendReplacement(result, replaceTo.toString() + " "); } String cleanFromClause = fromClauseMather.appendTail(result).toString(); String finalResult = StringUtils.replace(toReturn, selection, cleanFromClause); return new StringBuilder(finalResult); }
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 w w w. j a v a 2s . co 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:net.gcolin.simplerepo.maven.Resolver.java
public static String resolve(String value, Properties props, Model model) { if (value == null) { return null; }//from w ww. j a va 2 s . com Matcher matcher = VAR_PATTERN.matcher(value); StringBuffer result = new StringBuffer(); while (matcher.find()) { String expr = matcher.group(1); if (props.containsKey(expr)) { matcher.appendReplacement(result, props.getProperty(expr)); } else { if (expr.startsWith("project.")) { expr = expr.substring(8); } try { matcher.appendReplacement(result, String.valueOf(BeanUtils.getProperty(model, expr))); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new IllegalArgumentException(e); } } } matcher.appendTail(result); return result.toString(); }
From source file:io.renren.modules.test.jmeter.report.LocalReportGenerator.java
/** * <p>/*from ww w . j a va 2s . c om*/ * Gets the name of property setter from the specified key. * </p> * <p> * E.g : with key set_granularity, returns setGranularity (camel case) * </p> * * @param propertyKey * the property key * @return the name of the property setter */ private static String getSetterName(String propertyKey) { Matcher matcher = POTENTIAL_CAMEL_CASE_PATTERN.matcher(propertyKey); StringBuffer buffer = new StringBuffer(); // NOSONAR Unfortunately Matcher does not support StringBuilder while (matcher.find()) { matcher.appendReplacement(buffer, matcher.group(1).toUpperCase()); } matcher.appendTail(buffer); return buffer.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 w w w . j ava 2 s. c o 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:com.jaeksoft.searchlib.util.StringUtils.java
public static final String removeTag(String text, String[] allowedTags) { if (allowedTags == null) text = replaceConsecutiveSpaces(text, " "); StringBuffer sb = new StringBuffer(); Matcher matcher; synchronized (removeTagPattern) { matcher = removeTagPattern.matcher(text); }/*from ww w . j ava 2s. c om*/ while (matcher.find()) { boolean allowed = false; String group = matcher.group(); if (allowedTags != null) { for (String tag : allowedTags) { if (tag.equals(group)) { allowed = true; break; } } } matcher.appendReplacement(sb, allowed ? group : ""); } matcher.appendTail(sb); return sb.toString(); }