java.util.regex.Matcher.quoteReplacement()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(423)

本文整理了Java中java.util.regex.Matcher.quoteReplacement()方法的一些代码示例,展示了Matcher.quoteReplacement()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Matcher.quoteReplacement()方法的具体详情如下:
包路径:java.util.regex.Matcher
类名称:Matcher
方法名:quoteReplacement

Matcher.quoteReplacement介绍

[英]Returns a replacement string for the given one that has all backslashes and dollar signs escaped.
[中]

代码示例

代码示例来源:origin: apache/kafka

private String escape(String jsonStringValue) {
  String replace1 = DOUBLEQUOTE.matcher(jsonStringValue).replaceAll(Matcher.quoteReplacement("\\\""));
  return BACKSLASH.matcher(replace1).replaceAll(Matcher.quoteReplacement("\\\\"));
}

代码示例来源:origin: apache/incubator-dubbo

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();
}

代码示例来源:origin: apache/nifi

/**
 * 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;
}

代码示例来源:origin: pmd/pmd

DocumentType doctype = n.getOwnerDocument().getDoctype();
result = result.replaceAll(Matcher.quoteReplacement("&"), "&amp;");
result = result.replaceAll(Matcher.quoteReplacement("<"), "&lt;");
result = result.replaceAll(Matcher.quoteReplacement(">"), "&gt;");
if (withQuotes) {
  result = result.replaceAll(Matcher.quoteReplacement("\""), "&quot;");
  result = result.replaceAll(Matcher.quoteReplacement("'"), "&apos;");
    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 + ";");

代码示例来源:origin: apache/incubator-dubbo

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();
}

代码示例来源:origin: apache/kafka

public String saslName(String username) {
  String replace1 = EQUAL.matcher(username).replaceAll(Matcher.quoteReplacement("=3D"));
  return COMMA.matcher(replace1).replaceAll(Matcher.quoteReplacement("=2C"));
}

代码示例来源:origin: Alluxio/alluxio

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));

代码示例来源:origin: gocd/gocd

/**
 * 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"));
}

代码示例来源:origin: lets-blade/blade

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();
}

代码示例来源:origin: apache/kafka

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("="));
}

代码示例来源:origin: lets-blade/blade

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();
}

代码示例来源:origin: checkstyle/checkstyle

/**
 * 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);
}

代码示例来源:origin: google/ExoPlayer

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();
}

代码示例来源:origin: cucumber/cucumber-jvm

private String replaceMatchWith(String name, String replacement) {
    Matcher matcher = pattern.matcher(name);
    String quotedReplacement = Matcher.quoteReplacement(replacement);
    return matcher.replaceAll(quotedReplacement);
  }
}

代码示例来源:origin: looly/hutool

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();
}

代码示例来源:origin: stanfordnlp/CoreNLP

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));
}

代码示例来源:origin: looly/hutool

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();
}

代码示例来源:origin: oracle/helidon

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;
}

代码示例来源:origin: lets-blade/blade

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());
}

代码示例来源:origin: stackoverflow.com

public String replace(CharSequence target, CharSequence replacement) {
  return Pattern
      .compile(target.toString(), Pattern.LITERAL)
      .matcher(this )
      .replaceAll(
          Matcher.quoteReplacement(replacement.toString()));
}

相关文章