简体   繁体   English

我想在Java中使用String.replaceAll(regex,regex)而不是(regex,String)

[英]I want to do String.replaceAll(regex, regex) instead of (regex, String) in Java

Example: 例:

input = "10N10N";
input = input.replaceAll("1|N", "N|1"); // syntax not correct

expected output: N01N01 预期产量: N01N01

What I'm asking is for a single-lined iterator that replaces all "1" with "N" and all "N" with "1". 我要问的是单行迭代器将所有“1”替换为“N”而所有“N”替换为“1”。

Since Java 9 Matcher class contains replaceAll​(Function<MatchResult,String> replacer) where you can specify dynamically what should be used as replacement based on current match. 由于Java 9 Matcher类包含replaceAll​(Function<MatchResult,String> replacer) ,您可以根据当前匹配动态指定应该用作替换的内容。 So your code may look like: 所以你的代码可能如下所示:

Map<String, String> replacementsMap = Map.ofEntries(
        Map.entry("1", "N"),
        Map.entry("N", "1")
);

String input = "10N10N";
Pattern p = Pattern.compile("1|N");
Matcher m = p.matcher(input);
String replaced = m.replaceAll(match -> replacementsMap.get(match.group()));
System.out.println(replaced);

Output: N01N01 . 输出: N01N01


In pre Java 9 you can use Matcher#appendReplacement and Matcher#appendTail instead of replaceAll like 在Java 9之前,您可以使用Matcher#appendReplacementMatcher#appendTail代替replaceAll

//create Pattern p, Matcher m and replacement map
StringBuffer sb = new StringBuffer();
while(m.find()){
    m.appendReplacement(sb, replacementsMap.get(m.group()));
}
m.appendTail(sb);
String replaced = sb.toString();

If you are willing to use external libraries then Apache Commons - Lang include StringUtils class with replaceEach(String text, String[] searchList, String[] replacementList) method. 如果您愿意使用外部库,那么Apache Commons - Lang包含StringUtils类和replaceEach(String text, String[] searchList, String[] replacementList)方法。 You can use it like: 您可以像以下一样使用它:

String input = "10N10N";
String replaced = StringUtils.replaceEach(input, new String[] {"1","N"}, new String[] {"N","1"});
System.out.println(replaced);//N01N01

I'd advise you to prefer not going the regex way. 我建议你不要采用正则表达式。 Instead, why not go this way: 相反,为什么不这样走:

  • Replace all 1 with _ (OR any not-present char in the input string), then replace N by 1, and further replace _ by N. 将所有1替换为_(或输入字符串中的任何不存在的char),然后将N替换为1,并进一步将_替换为N.

This can be easily done by multiple replaces, all within a single line. 这可以通过多次替换轻松完成,所有这些都在一行内完成。

Try to use : 尝试使用:

input = input.replace("1", "-")//replace all 1 by - 
        .replace("N", "1")     //replace all N by 1
        .replace("-", "N");    //replace all - by N

More secure solution, if you are using Java 8 you can use : 更安全的解决方案,如果您使用的是Java 8,则可以使用:

String result = input.chars()
        .map(x -> x == 'N' ? '1' : x == '1' ? 'N' : x)
        .collect(StringBuilder::new,
                StringBuilder::appendCodePoint,
                StringBuilder::append
        ).toString();

Output 产量

N01N01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM