private String escape(String jsonStringValue) { String replace1 = DOUBLEQUOTE.matcher(jsonStringValue).replaceAll(Matcher.quoteReplacement("\\\"")); return BACKSLASH.matcher(replace1).replaceAll(Matcher.quoteReplacement("\\\\")); }
public static String replaceProperty(String expression, Map<String, String> params) { if (expression == null || expression.length() == 0 || expression.indexOf('$') < 0) { return expression; } Matcher matcher = VARIABLE_PATTERN.matcher(expression); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String key = matcher.group(1); String value = System.getProperty(key); if (value == null && params != null) { value = params.get(key); } if (value == null) { value = ""; } matcher.appendReplacement(sb, Matcher.quoteReplacement(value)); } matcher.appendTail(sb); return sb.toString(); }
/** * If we have a '$' followed by anything other than a number, then escape * it. E.g., '$d' becomes '\$d' so that it can be used as a literal in a * regex. */ private static String normalizeReplacementString(String replacement) { String replacementFinal = replacement; if (REPLACEMENT_NORMALIZATION_PATTERN.matcher(replacement).find()) { replacementFinal = Matcher.quoteReplacement(replacement); } return replacementFinal; }
DocumentType doctype = n.getOwnerDocument().getDoctype(); result = result.replaceAll(Matcher.quoteReplacement("&"), "&"); result = result.replaceAll(Matcher.quoteReplacement("<"), "<"); result = result.replaceAll(Matcher.quoteReplacement(">"), ">"); if (withQuotes) { result = result.replaceAll(Matcher.quoteReplacement("\""), """); result = result.replaceAll(Matcher.quoteReplacement("'"), "'"); Node firstChild = item.getFirstChild(); if (firstChild != null) { result = result.replaceAll(Matcher.quoteReplacement(firstChild.getNodeValue()), "&" + entityName + ";"); } else { Matcher m = Pattern .compile(Matcher.quoteReplacement("<!ENTITY " + entityName + " ") + "[']([^']*)[']>") .matcher(internalSubset); if (m.find()) { result = result.replaceAll(Matcher.quoteReplacement(m.group(1)), "&" + entityName + ";");
public static String replaceProperty(String expression, Map<String, String> params) { if (expression == null || expression.length() == 0 || expression.indexOf('$') < 0) { return expression; } Matcher matcher = VARIABLE_PATTERN.matcher(expression); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String key = matcher.group(1); String value = System.getProperty(key); if (value == null && params != null) { value = params.get(key); } if (value == null) { value = ""; } matcher.appendReplacement(sb, Matcher.quoteReplacement(value)); } matcher.appendTail(sb); return sb.toString(); }
public String saslName(String username) { String replace1 = EQUAL.matcher(username).replaceAll(Matcher.quoteReplacement("=3D")); return COMMA.matcher(replace1).replaceAll(Matcher.quoteReplacement("=2C")); }
Matcher matcher = CONF_REGEX.matcher(base); while (matcher.find()) { String match = matcher.group(2).trim(); if (!seen.add(match)) { throw new RuntimeException(ExceptionMessage.KEY_CIRCULAR_DEPENDENCY.getMessage(match)); .UNDEFINED_CONFIGURATION_KEY.getMessage(match)); LOG.debug("Replacing {} with {}", matcher.group(1), value); resolved = resolved.replaceFirst(REGEX_STRING, Matcher.quoteReplacement(value));
/** * Surrounds a string with double quotes if it is not already surrounded by single or double quotes, or if it contains * unescaped spaces, single quotes, or double quotes. When surrounding with double quotes, this method will only escape * double quotes in the String. * * This method assumes the argument is well-formed if it was already surrounded by either single or double quotes. * * @param argument String to quote * @return the quoted String, if not already quoted */ public static String quoteArgument(String argument) { if (QUOTED_STRING.matcher(argument).matches() || !UNESCAPED_SPACE_OR_QUOTES.matcher(argument).find()) { // assume the argument is well-formed if it's already quoted or if there are no unescaped spaces or quotes return argument; } return String.format("\"%s\"", DOUBLE_QUOTE.matcher(argument).replaceAll(Matcher.quoteReplacement("\\") + "$1")); }
private String escapeComments(final String s) { final Matcher m = P_COMMENTS.matcher(s); final StringBuffer buf = new StringBuffer(); if (m.find()) { final String match = m.group(1); //(.*?) m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->")); } m.appendTail(buf); return buf.toString(); }
public String username(String saslName) { String username = EQUAL_TWO_C.matcher(saslName).replaceAll(Matcher.quoteReplacement(",")); if (EQUAL_THREE_D.matcher(username).replaceAll(Matcher.quoteReplacement("")).indexOf('=') >= 0) { throw new IllegalArgumentException("Invalid username: " + saslName); } return EQUAL_THREE_D.matcher(username).replaceAll(Matcher.quoteReplacement("=")); }
private String escapeComments(final String s) { final Matcher m = P_COMMENTS.matcher(s); final StringBuffer buf = new StringBuffer(); if (m.find()) { final String match = m.group(1); //(.*?) m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->")); } m.appendTail(buf); return buf.toString(); }
/** * Method returns regular expression pattern given key name. * * @param keyName * key name to look for * @return regular expression pattern given key name */ private static Pattern getKeyPattern(String keyName) { final String keyPatternString = "^" + SPACE_PATTERN.matcher(keyName) .replaceAll(Matcher.quoteReplacement("\\\\ ")) + "[\\s:=].*$"; return Pattern.compile(keyPatternString); }
private static String replaceVariableReferences( String string, Map<String, String> variableDefinitions) { Matcher matcher = REGEX_VARIABLE_REFERENCE.matcher(string); // TODO: Replace StringBuffer with StringBuilder once Java 9 is available. StringBuffer stringWithReplacements = new StringBuffer(); while (matcher.find()) { String groupName = matcher.group(1); if (variableDefinitions.containsKey(groupName)) { matcher.appendReplacement( stringWithReplacements, Matcher.quoteReplacement(variableDefinitions.get(groupName))); } else { // The variable is not defined. The value is ignored. } } matcher.appendTail(stringWithReplacements); return stringWithReplacements.toString(); }
private String replaceMatchWith(String name, String replacement) { Matcher matcher = pattern.matcher(name); String quotedReplacement = Matcher.quoteReplacement(replacement); return matcher.replaceAll(quotedReplacement); } }
private String escapeComments(final String s) { final Matcher m = P_COMMENTS.matcher(s); final StringBuffer buf = new StringBuffer(); if (m.find()) { final String match = m.group(1); // (.*?) m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->")); } m.appendTail(buf); return buf.toString(); }
public void bindStringRegex(String var, String regex) { // Enforce requirements on variable names ($alphanumeric_) if (!STRING_REGEX_VAR_NAME_PATTERN.matcher(var).matches()) { throw new IllegalArgumentException("StringRegex binding error: Invalid variable name " + var); } Pattern varPattern = Pattern.compile(Pattern.quote(var)); String replace = Matcher.quoteReplacement(regex); stringRegexVariables.put(var, new Pair<>(varPattern, replace)); }
private String escapeComments(final String s) { final Matcher m = P_COMMENTS.matcher(s); final StringBuffer buf = new StringBuffer(); if (m.find()) { final String match = m.group(1); // (.*?) m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->")); } m.appendTail(buf); return buf.toString(); }
static String[] toArray(String stringValue) { String[] values = SPLIT_PATTERN.split(stringValue, -1); for (int i = 0; i < values.length; i++) { String value = values[i]; values[i] = ESCAPED_COMMA_PATTERN.matcher(value).replaceAll(Matcher.quoteReplacement(",")); } return values; }
private String validateEntities(final String s) { StringBuffer buf = new StringBuffer(); // validate entities throughout the string Matcher m = P_VALID_ENTITIES.matcher(s); while (m.find()) { final String one = m.group(1); //([^&;]*) final String two = m.group(2); //(?=(;|&|$)) m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two))); } m.appendTail(buf); return encodeQuotes(buf.toString()); }
public String replace(CharSequence target, CharSequence replacement) { return Pattern .compile(target.toString(), Pattern.LITERAL) .matcher(this ) .replaceAll( Matcher.quoteReplacement(replacement.toString())); }