Replace While Pattern is Found

2020-04-23 07:09发布

I'm trying to go through a string and replace all instances of a regex-matching string. For some reason when I use if then it will work and replace just one string instance of a regex-match. When I change the if to while then it does some weird replacement over itself and makes a mess on the first regex-matching string while not even touching the others...

        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(docToProcess);
        while (matcher.find()) {
            start = matcher.start();
            end = matcher.end();
            match = docToProcess.substring(start, end);
            stringBuilder.replace(start, end, createRef(match));
            docToProcess = stringBuilder.toString();
        }

3条回答
看我几分像从前
2楼-- · 2020-04-23 07:40

Not sure exactly what problem you got but maybe this example will help a little:

I want to change names in sentence like:

  • Jack -> Albert
  • Albert -> Paul
  • Paul -> Jack

We can do this with little help of appendReplacement and appendTail methods from Matcher class

//this method can use Map<String,String>, or maybe even be replaced with Map.get(key)
static String getReplacement(String name) { 
    if ("Jack".equals(name))
        return "Albert";
    else if ("Albert".equals(name))
        return "Paul";
    else
        return "Jack";
}

public static void main(String[] args) {

    String sentence = "Jack and Albert are goint to see Paul. Jack is tall, " +
            "Albert small and Paul is not in home.";

    Matcher m = Pattern.compile("Jack|Albert|Paul").matcher(sentence);

    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, getReplacement(m.group()));
    }
    m.appendTail(sb);

    System.out.println(sb);
}

Output:

Albert and Paul are goint to see Jack. Albert is tall, Paul small and Jack is not in home.
查看更多
够拽才男人
3楼-- · 2020-04-23 07:41

Aside from the sysouts I only added the last assignment. See if it helps:

// your snippet:    
pattern = Pattern.compile(regex);
matcher = pattern.matcher(docToProcess);
while (matcher.find()) {
    start = matcher.start();
    end = matcher.end();
    match = docToProcess.substring(start, end);
    String rep = createRef(match);
    stringBuilder.replace(start, end, rep);
    docToProcess = stringBuilder.toString();
    // my addition:
    System.out.println("Found:         '" + matcher.group() + "'");
    System.out.println("Replacing with: '" + rep + "'");
    System.out.println(" --> " + docToProcess);
    matcher = pattern.matcher(docToProcess);
}
查看更多
ら.Afraid
4楼-- · 2020-04-23 08:02

If createRef(match) returns a string which is not the same length as (end - start) then the indexes you are using in docToProcess.substring(start, end) will potentially overlap.

查看更多
登录 后发表回答