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

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

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

Matcher.replaceFirst介绍

[英]Replaces the first occurrence of this matcher's pattern in the input with a given string.
[中]用给定字符串替换输入中第一次出现的匹配器模式。

代码示例

代码示例来源:origin: prestodb/presto

private String replaceZeroOffsetAsZIfNecessary(String text)
{
  if (replaceZeroOffsetAsZ) {
    return ISO8601_UTC_ZERO_OFFSET_SUFFIX_REGEX.matcher(text).replaceFirst("Z");
  }
  return text;
}

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

Pattern p = Pattern.compile("(\\d)(.*)(\\d)");
String input = "6 example input 4";
Matcher m = p.matcher(input);
if (m.find()) {
  // replace first number with "number" and second number with the first
  String output = m.replaceFirst("number $2$1");  // number 46
}

代码示例来源:origin: linlinjava/litemall

/**
 * Replace the first subsequence of the input sequence that matches the
 * regex with the given replacement string.
 *
 * @param input       The input.
 * @param regex       The regex.
 * @param replacement The replacement string.
 * @return the string constructed by replacing the first matching
 * subsequence by the replacement string, substituting captured
 * subsequences as needed
 */
public static String getReplaceFirst(final String input,
                   final String regex,
                   final String replacement) {
  if (input == null) return "";
  return Pattern.compile(regex).matcher(input).replaceFirst(replacement);
}

代码示例来源:origin: k9mail/k-9

public static String stripSignature(String content) {
    if (DASH_SIGNATURE_PLAIN.matcher(content).find()) {
      content = DASH_SIGNATURE_PLAIN.matcher(content).replaceFirst("\r\n");
    }
    return content;
  }
}

代码示例来源:origin: org.apache.lucene/lucene-analyzers-common

@Override
public boolean incrementToken() throws IOException {
 if (!input.incrementToken()) return false;
 
 m.reset();
 if (m.find()) {
  // replaceAll/replaceFirst will reset() this previous find.
  String transformed = all ? m.replaceAll(replacement) : m.replaceFirst(replacement);
  termAtt.setEmpty().append(transformed);
 }
 return true;
}

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

/**
 * Replaces a found pattern in a word and returns a transformed word. Null is pattern does not match.
 */
private static String gsub(String word, String rule, String replacement)
{
  Pattern pattern = Pattern.compile(rule, Pattern.CASE_INSENSITIVE);
  Matcher matcher = pattern.matcher(word);
  return matcher.find() ? matcher.replaceFirst(replacement) : null;
}

代码示例来源:origin: Graylog2/graylog2-server

public Result runExtractor(String value) {
  final Matcher matcher = pattern.matcher(value);
  final boolean found = matcher.find();
  if (!found) {
    return null;
  }
  final int start = matcher.groupCount() > 0 ? matcher.start(1) : -1;
  final int end = matcher.groupCount() > 0 ? matcher.end(1) : -1;
  final String s;
  try {
    s = replaceAll ? matcher.replaceAll(replacement) : matcher.replaceFirst(replacement);
  } catch (Exception e) {
    throw new RuntimeException("Error while trying to replace string", e);
  }
  return new Result(s, start, end);
}

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

/**
 * Replaces the first match for {@code regularExpression} within this string with the given
 * {@code replacement}.
 * See {@link Pattern} for regular expression syntax.
 *
 * <p>If the same regular expression is to be used for multiple operations, it may be more
 * efficient to reuse a compiled {@code Pattern}.
 *
 * @throws PatternSyntaxException
 *             if the syntax of the supplied regular expression is not
 *             valid.
 * @throws NullPointerException if {@code regularExpression == null}
 * @see Pattern
 * @since 1.4
 */
public String replaceFirst(String regularExpression, String replacement) {
  return Pattern.compile(regularExpression).matcher(this).replaceFirst(replacement);
}

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

/**
 * Replace the matches of the from pattern in the base string with the value
 * of the to string.
 * @param base the string to transform
 * @param from the pattern to look for in the base string
 * @param to the string to replace matches of the pattern with
 * @param repeat whether the substitution should be repeated
 * @return
 */
static String replaceSubstitution(String base, Pattern from, String to,
                 boolean repeat) {
  Matcher match = from.matcher(base);
  if (repeat) {
    return match.replaceAll(to);
  } else {
    return match.replaceFirst(to);
  }
}

代码示例来源:origin: zhangyd-c/springboot-learning

/**
 * 替换段落里面的变量
 * 
 * @param para
 *            要替换的段落
 * @param params
 *            参数
 */
private static void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
  List<XWPFRun> runs;
  Matcher matcher;
  if (matcher(para.getParagraphText()).find()) {
    runs = para.getRuns();
    for (int i = 0; i < runs.size(); i++) {
      XWPFRun run = runs.get(i);
      String runText = run.toString();
      matcher = matcher(runText);
      if (matcher.find()) {
        while ((matcher = matcher(runText)).find()) {
          runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
        }
        // 直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,
        // 所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。
        para.removeRun(i);
        para.insertNewRun(i).setText(runText);
      }
    }
  }
}

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

Pattern rePron = Pattern.compile("\\[(.*)\\]"); // pronunciation in square brackets
StringTokenizer st = new StringTokenizer(newText);
ArrayList<Element> newTokens = new ArrayList<Element>();
  Matcher remPron = rePron.matcher(text);
  if (remPron.find()) {
    String pron = remPron.group(1); // would be $1 in perl
    text = rePron.matcher(text).replaceFirst(""); // delete pronunciation from word
    newT.setAttribute("ph", pron);

代码示例来源:origin: networknt/light-4j

/**
 * <p>Converts <code>http</code> scheme to <code>https</code>.</p>
 * <code>http://www.example.com/ &rarr; https://www.example.com/</code>
 * @return this instance
 */
public URLNormalizer secureScheme() {
  Matcher m = PATTERN_SCHEMA.matcher(url);
  if (m.find()) {
    String schema = m.group(1);
    if ("http".equalsIgnoreCase(schema)) {
      url = m.replaceFirst(schema + "s$2");
    }
  }
  return this;
}
/**

代码示例来源:origin: alibaba/jetcache

protected String removeHiddenPackage(String[] hiddenPackages, String packageOrFullClassName) {
  if (hiddenPackages != null && packageOrFullClassName != null) {
    for (String p : hiddenPackages) {
      if (p != null && packageOrFullClassName.startsWith(p)) {
        packageOrFullClassName = Pattern.compile(p, Pattern.LITERAL).matcher(
            packageOrFullClassName).replaceFirst("");
        if (packageOrFullClassName.length() > 0 && packageOrFullClassName.charAt(0) == '.') {
          packageOrFullClassName = packageOrFullClassName.substring(1);
        }
        return packageOrFullClassName;
      }
    }
  }
  return packageOrFullClassName;
}

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

private static String removeComments(String line) {
 Matcher m = commentPattern.matcher(line);
 line = m.replaceFirst("");
 Matcher m1 = escapedCommentCharacterPattern.matcher(line);
 line = m1.replaceAll(commentIntroducingCharacter);
 return line;
}

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

Pattern rePron = Pattern.compile("\\[(.*)\\]"); // pronunciation in square brackets
StringTokenizer st = new StringTokenizer(newText);
ArrayList<Element> newTokens = new ArrayList<Element>();
  Matcher remPron = rePron.matcher(text);
  if (remPron.find()) {
    String pron = remPron.group(1); // would be $1 in perl
    text = rePron.matcher(text).replaceFirst(""); // delete pronunciation from word
    newT.setAttribute("ph", pron);

代码示例来源:origin: jenkinsci/jenkins

Matcher m = TIMESTAMP_ELT.matcher(xml);
if (!m.find()) {
  System.err.println(buildXml + " did not contain <timestamp> as expected");
  continue;
xml = m.replaceFirst("  <number>" + number + "</number>" + nl);
m = ID_ELT.matcher(xml);
String id;
if (m.find()) {
  id = m.group(1);
  xml = m.replaceFirst("");
} else {

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

private String getVersionHelper(Path jreDir, String regex, boolean javaVersion, boolean altjvm) {
  String version = getVersionString(jreDir, altjvm);
  version = version.replaceAll("\n", "");
  Matcher matcher = Pattern.compile(regex).matcher(version);
  if (!matcher.matches()) {
    return "Could not get " + (javaVersion ? "java" : "dce") +
        "version of " + jreDir.toAbsolutePath() + ".";
  }
  version = matcher.replaceFirst("$1");
  return version;
}

代码示例来源:origin: aws/aws-sdk-java

private String sanitize(String input) {
  return PATTERN.matcher(input).replaceFirst("Z");
}

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

Pattern rePron = Pattern.compile("\\[(.*)\\]"); // pronunciation in square brackets
StringTokenizer st = new StringTokenizer(newText);
ArrayList newTokens = new ArrayList();
  Matcher remPron = rePron.matcher(text);
  if (remPron.find()) {
    String pron = remPron.group(1); // would be $1 in perl
    text = rePron.matcher(text).replaceFirst(""); // delete pronunciation from word
    newT.setAttribute("ph", pron);

代码示例来源:origin: networknt/light-4j

/**
 * <p>Converts <code>https</code> scheme to <code>http</code>.</p>
 * <code>https://www.example.com/ &rarr; http://www.example.com/</code>
 * @return this instance
 */
public URLNormalizer unsecureScheme() {
  Matcher m = PATTERN_SCHEMA.matcher(url);
  if (m.find()) {
    String schema = m.group(1);
    if ("https".equalsIgnoreCase(schema)) {
      url = m.replaceFirst(StringUtils.stripEnd(schema, "Ss") + "$2");
    }
  }
  return this;
}
/**

相关文章