简体   繁体   English

在java模式匹配器(regex)中如何用不同的文本迭代和替换每个文本

[英]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:但是 while 循环在一场比赛后退出,并且"test"在任何地方都被替换,如下所示:

{"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":然后,由于$$ ,我在线程“main”中遇到异常:

java.lang.IllegalArgumentException: Illegal group reference at java.util.regex.Matcher.appendReplacement(Matcher.java:857)** java.lang.IllegalArgumentException: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:对于 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:对于 Java 9 及更高版本:

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.如果您将$5 $2替换$2 ,这就是我认为您的目标。

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" };换句话说,您必须将替换数组编写为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"}

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

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