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

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

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

Matcher.appendReplacement介绍

[英]Appends a literal part of the input plus a replacement for the current match to a given StringBuffer. The literal part is exactly the part of the input between the previous match and the current match. The method can be used in conjunction with #find() and #appendTail(StringBuffer) to walk through the input and replace all occurrences of the Pattern with something else.
[中]

代码示例

代码示例来源:origin: shuzheng/zheng

/**
 * 驼峰转下划线,效率比上面高
 * @param str
 * @return
 */
public static String humpToLine(String str) {
  Matcher matcher = humpPattern.matcher(str);
  StringBuffer sb = new StringBuffer();
  while (matcher.find()) {
    matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
  }
  matcher.appendTail(sb);
  return sb.toString();
}

代码示例来源: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/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: hs-web/hsweb-framework

public static String parse ( String input ) {
  
  Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}", Pattern.CASE_INSENSITIVE);
  Matcher matcher = pattern.matcher(input);
  
  PathFormat.currentDate = new Date();
  
  StringBuffer sb = new StringBuffer();
  
  while ( matcher.find() ) {
    
    matcher.appendReplacement(sb, PathFormat.getString(matcher.group(1)) );
    
  }
  
  matcher.appendTail(sb);
  
  return sb.toString();
}

代码示例来源:origin: shuzheng/zheng

/**
 * 下划线转驼峰
 * @param str
 * @return
 */
public static String lineToHump(String str) {
  if (null == str || "".equals(str)) {
    return str;
  }
  str = str.toLowerCase();
  Matcher matcher = linePattern.matcher(str);
  StringBuffer sb = new StringBuffer();
  while (matcher.find()) {
    matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
  }
  matcher.appendTail(sb);
  str = sb.toString();
  str = str.substring(0, 1).toUpperCase() + str.substring(1);
  return str;
}

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

StringBuffer resultString = new StringBuffer();
Pattern regex = Pattern.compile("regex");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
 // You can vary the replacement text for each match on-the-fly
 regexMatcher.appendReplacement(resultString, "replacement");
}
regexMatcher.appendTail(resultString);

代码示例来源: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: stackoverflow.com

Pattern p = Pattern.compile("(\\d{1,2})");
 Matcher m = p.matcher("12 54 1 65");
 StringBuffer s = new StringBuffer();
 while (m.find())
   m.appendReplacement(s, String.valueOf(3 * Integer.parseInt(m.group(1))));
 System.out.println(s.toString());

代码示例来源: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: 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: btraceio/btrace

private int replaceSysProps(String str, StringBuffer sb) {
  int cnt = 0;
  Matcher m = SYSPROP_PTN.matcher(str);
  while (m.find()) {
    String key = m.group(1);
    String val = System.getProperty(key);
    if (val != null) {
      cnt++;
      m.appendReplacement(sb, val);
    } else {
      m.appendReplacement(sb, m.group(0));
    }
  }
  m.appendTail(sb);
  return cnt;
}

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

public static String manageAmpDiese(String s) {
  final Pattern pattern = Pattern.compile("\\&#([0-9]+);");
  final Matcher matcher = pattern.matcher(s);
  final StringBuffer result = new StringBuffer();
  while (matcher.find()) {
    final String num = matcher.group(1);
    final char c = (char) Integer.parseInt(num);
    matcher.appendReplacement(result, "" + c);
  }
  matcher.appendTail(result);
  return result.toString();
}

代码示例来源:origin: nutzam/nutz

public static String unicodeDecode(String s) {
  Matcher m = reUnicode.matcher(s);
  StringBuffer sb = new StringBuffer(s.length());
  while (m.find()) {
    m.appendReplacement(sb, Character.toString((char) Integer.parseInt(m.group(1), 16)));
  }
  m.appendTail(sb);
  return sb.toString();
}

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

public static String manageUnicodeNotationUplus(String s) {
  final Pattern pattern = Pattern.compile("\\<U\\+([0-9a-fA-F]{4,5})\\>");
  final Matcher matcher = pattern.matcher(s);
  final StringBuffer result = new StringBuffer();
  while (matcher.find()) {
    final String num = matcher.group(1);
    final int value = Integer.parseInt(num, 16);
    matcher.appendReplacement(result, new String(Character.toChars(value)));
  }
  matcher.appendTail(result);
  return result.toString();
}

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

Map<String,String> tokens = new HashMap<String,String>();
tokens.put("cat", "Garfield");
tokens.put("beverage", "coffee");

String template = "%cat% really needs some %beverage%.";

// Create pattern of the format "%(cat|beverage)%"
String patternString = "%(" + StringUtils.join(tokens.keySet(), "|") + ")%";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(template);

StringBuffer sb = new StringBuffer();
while(matcher.find()) {
  matcher.appendReplacement(sb, tokens.get(matcher.group(1)));
}
matcher.appendTail(sb);

System.out.println(sb.toString());

代码示例来源:origin: siacs/Conversations

private static String unescape(final CharSequence localPart) {
  if (localPart != null) {
    final Matcher matcher = UNESCAPE_PATTERN.matcher(localPart);
    final StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
      final char c = (char) Integer.parseInt(matcher.group(1), 16);
      if (c == '\\') {
        matcher.appendReplacement(sb, "\\\\");
      } else {
        matcher.appendReplacement(sb, String.valueOf(c));
      }
    }
    matcher.appendTail(sb);
    return sb.toString();
  }
  return null;
}

代码示例来源:origin: hs-web/hsweb-framework

public static String parse ( String input, String filename ) {

  Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}", Pattern.CASE_INSENSITIVE);
  Matcher matcher = pattern.matcher(input);
  String matchStr = null;
  
  PathFormat.currentDate = new Date();
  
  StringBuffer sb = new StringBuffer();
  
  while ( matcher.find() ) {
    
    matchStr = matcher.group( 1 );
    if (matchStr.contains("filename")) {
      filename = filename.replace( "$", "\\$" ).replaceAll( "[\\/:*?\"<>|]", "" );
      matcher.appendReplacement(sb, filename );
    } else {
      matcher.appendReplacement(sb, PathFormat.getString(matchStr) );
    }
    
  }
  
  matcher.appendTail(sb);
  
  return sb.toString();
}

代码示例来源:origin: GitLqr/LQRWeChat

private String getEmotion(String var1) {
  Pattern var2 = Pattern.compile("\\[/u([0-9A-Fa-f]+)\\]");
  Matcher var3 = var2.matcher(var1);
  StringBuffer var4 = new StringBuffer();
  while (var3.find()) {
    int var5 = Integer.parseInt(var3.group(1), 16);
    var3.appendReplacement(var4, String.valueOf(Character.toChars(var5)));
  }
  var3.appendTail(var4);
  return var4.toString();
}

代码示例来源:origin: GitLqr/LQRWeChat

private String getEmotion(String var1) {
  Pattern var2 = Pattern.compile("\\[/u([0-9A-Fa-f]+)\\]");
  Matcher var3 = var2.matcher(var1);
  StringBuffer var4 = new StringBuffer();
  while (var3.find()) {
    int var5 = Integer.parseInt(var3.group(1), 16);
    var3.appendReplacement(var4, String.valueOf(Character.toChars(var5)));
  }
  var3.appendTail(var4);
  return var4.toString();
}

代码示例来源: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();
}

相关文章