简体   繁体   中英

In java pattern matcher(regex) how to iterate and replace each text with different text

I want to check for pattern matching, and if the pattern matches, then I wanted to replace those text matches with the element in the test array at the given index.

public class test {
    public static void main(String[] args) {
        String[] test={"one","two","three","four"}
        Pattern pattern = Pattern.compile("\\$(\\d)+");
        String text="{\"test1\":\"$1\",\"test2\":\"$5\",\"test3\":\"$3\",\"test4\":\"$4\"}";
        Matcher matcher = pattern.matcher(text);

    

while(matcher.find()) {
       System.out.println(matcher.groupCount());
       System.out.println(matcher.replaceAll("test"));
    }
  System.out.println(text);
}

}

I want the end result text string to be in this format:

{\"test1\":\"one\",\"test2\":\"$two\",\"test3\":\"three\",\"test4\":\"four\"}

but the while loop is exiting after one match and "test" is replaced everywhere like this:

{"test1":"test","test2":"test","test3":"test","test4":"test"}

Using the below code I got the result:

  public class test {
        
        public static void main(String[] args) {
            String[] test={"one","two","three","four"};
            Pattern pattern = Pattern.compile("\\$(\\d)+");
            String text="{\"test1\":\"$1\",\"test2\":\"$2\",\"test3\":\"$3\",\"test4\":\"$4\"}";
            Matcher m = pattern.matcher(text);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, test[Integer.parseInt(m.group(1)) - 1]);
        }
        m.appendTail(sb);
        System.out.println(sb.toString());
    
    }
    }

But, if I have a replacement text array like this,

String[] test={"$$one","two","three","four"};

then, because of the $$ , I am getting an exception in thread "main":

java.lang.IllegalArgumentException: Illegal group reference at java.util.regex.Matcher.appendReplacement(Matcher.java:857)**

The following line is your problem:

System.out.println(matcher.replaceAll("test"));

If you remove it the loop will walk through all matches.

As a solution for your problem, you could replace the loop with something like this:

For Java 8:

StringBuffer out = new StringBuffer();
while (matcher.find()) {
    String r = test[Integer.parseInt(matcher.group(1)) - 1];
    matcher.appendReplacement(out, r);
}
matcher.appendTail(out);
System.out.println(out.toString());

For Java 9 and above:

String x = matcher.replaceAll(match -> test[Integer.parseInt(match.group(1)) - 1]);
System.out.println(x);

This only works, if you replace the $5 with $2 which is what I would assume is your goal.

Concerning the $ signs in the replacement string, the documentation states:

A dollar sign ($) may be included as a literal in the replacement string by preceding it with a backslash (\\$).

In other words, you must write your replacement array as String[] test = { "\\\\$\\\\$one", "two", "three", "four" };

I can do a regex solution if you like, but this is much easier (assuming this is the desired output).

      int count = 1;
      for (String s : test) {
         text = text.replace("$" + count++, s);
      }
      System.out.println(text);

It prints.

{"test1":"one","test2":"two","test3":"three","test4":"four"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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