Java多次替换单次传递

Dar*_*one 11 java regex

我正在尝试在newick格式化的树上翻译节点,而我在更换权限方面遇到了麻烦.说我有HashMap:

"(1:" : "(30:"
",1:" : ",30:" 
"(30:" : "(6:"
",30:" : ",6:"
Run Code Online (Sandbox Code Playgroud)

而树:

(30:0.07,(1:0.06,2:0.76))
Run Code Online (Sandbox Code Playgroud)

传统智慧会暗示多重replaceAll,但这会带来一个问题:

replaceAll("(1:", "(30:") >> (30:0.07,(30:0.06,2:0.76))
replaceAll("(30:", "(6:") >> (6:0.07,(6:0.06,2:0.76))
Run Code Online (Sandbox Code Playgroud)

这里的问题是我们已经替换了之前被替换的节点.正确的树应如下所示:

(6:0.07,(30:0.06,2:0.76))
Run Code Online (Sandbox Code Playgroud)

现在我已经在Python中完成了这个:

def multiple_replace(taxa, text): 
    regex = re.compile("|".join(map(re.escape, taxa.keys())))
    return regex.sub(lambda mo: taxa[mo.group(0)], text) 
Run Code Online (Sandbox Code Playgroud)

但是我的Java实现遇到了问题:

private String convertTree (String treeOld, HashMap<String, String> conv) {
        Pattern pattern = Pattern.compile("\\(\\d+:|,\\d+:");
        Matcher matcher = pattern.matcher(treeOld);
        StringBuilder sbt = new StringBuilder(treeOld);
        while (matcher.find()) {
            String replace = conv.get(matcher.group());
            System.out.println(matcher.group() + "||" +replace + " || " + matcher.start() + ":"+matcher.end());
            sbt.delete(matcher.start(), matcher.end());
            sbt.insert(matcher.start(), replace);
        }
        return treeOld;

    }
Run Code Online (Sandbox Code Playgroud)

虽然替换似乎有效,但我无法使用不同大小的字符串使索引非常正确(如示例所示).有没有办法在Java中这样做?

Wik*_*żew 9

您可以使用Matcher#appendReplacement在匹配时修改字符串.

请注意,您的正则表达式可以简化[,(]\d+:为您的备用分支仅在第一个字符中不同([,(]匹配,或者().

这是一个IDEONE演示:

import java.util.*;
import java.util.regex.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String tree = "(30:0.07,(1:0.06,2:0.76))";
        HashMap<String, String> h = new HashMap<String, String>();
        h.put("(1:" , "(30:");
        h.put(",1:" , ",30:");
        h.put("(30:" , "(6:");
        h.put(",30:" , ",6:");
        System.out.println(convertTree(tree, h));

    }
    private static String convertTree(String treeOld, HashMap<String, String> conv) {
        Pattern pattern = Pattern.compile("[,(]\\d+:");  // Init the regex
        Matcher m = pattern.matcher(treeOld);            // Init the matcher
        StringBuffer result = new StringBuffer();        // Declare the string buffer (can be replaced with a string builder)
        while (m.find()) {                               // Iterate through matches
            if (conv.containsKey(m.group(0))) {          // Check if the key exists
                m.appendReplacement(result, conv.get(m.group(0))); // If yes, use the HashMap value
            }
            else {
                m.appendReplacement(result, m.group(0));  // Else, just reinsert the match value
            }
        }
        m.appendTail(result);        // Append what remains to the result
        return result.toString();

    }
}
Run Code Online (Sandbox Code Playgroud)


Dar*_*one 7

想出来,需要使用偏移值:

private String singlePassConvert (String text, HashMap<String, String> conv) {
        Pattern pattern = Pattern.compile("\\(\\d+:|,\\d+:");
        Matcher matcher = pattern.matcher(text);
        int offset = 0;
        while (matcher.find()) {
            String replace = conv.get(matcher.group());
            String head = (String) text.subSequence(0, matcher.start() + offset);
            String tail = (String) text.subSequence(matcher.end() + offset, text.length());

            text = head + conv.get(matcher.group()) + tail;

            if (matcher.group().length() > conv.get(matcher.group()).length()) {
                offset --;
            } else if (matcher.group().length() < conv.get(matcher.group()).length()) {
                offset ++;
            }
        }
        return text;

}
Run Code Online (Sandbox Code Playgroud)

但是,公平警告,因为这个实现没有使用StringBuilder,所以在大字符串上可能会很慢.

此外,偏移值仅适用于长度为+/- 1的差异,如果长度差异未知,则应进行修改.

  • @krzyk那是为什么?有人可以立即发布问题然后回答问题; 它实际上是[鼓励](http://stackoverflow.com/help/self-answer). (2认同)
  • @krzyk如果您认为某个问题可以帮助其他人,而您*有*答案,您可以立即同时发布.没有错. (2认同)