在正则表达式中使用反向引用来动态替换文本

在正则表达式中使用反向引用来动态替换文本

本文介绍了在正则表达式中使用反向引用来动态替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像整数一样使用$ 1值。

想法是用原始文本替换所有数字和等效数组值并创建一个新文本。

下面期望的结果应该是这是DBValue4,这是DBValue2,这是DBValue7


另外,有没有办法保存这些反向引用以供进一步使用?

I want to use the $1 value like an integer.
The idea is to replace all numbers from originaltext with the equivalent array values and create a new text.
The below desired outcome should be "This is DBValue4, This is DBValue2, This is DBValue7"
Also, is there a way to save these backreferences for further use?

String[] values = {"DBValue0","DBValue1","DBValue2","DBValue3","DBValue4","DBValue5","DBValue6","DBValue7","DBValue8","DBValue9","DBValue10"};
String originaltext = "This is 4, This is 2, This is 7";
text = originaltext.replaceAll("(\\d)","$1");
// want something like
text = originaltext.replaceAll("(\\d)",values[$1]);
//or
text = originaltext.replaceAll("(\\d)",values[Integer.parseInt("$1")]);


推荐答案

您可以使用模式 Matcher 之类似:

public static void main(String[] args) throws Exception {
    final String[] values = {"DBValue0", "DBValue1", "DBValue2", "DBValue3", "DBValue4", "DBValue5", "DBValue6", "DBValue7", "DBValue8", "DBValue9", "DBValue10"};
    final String originaltext = "This is 4, This is 2, This is 7";
    final Pattern pattern = Pattern.compile("(?<=This is )\\d++");
    final Matcher matcher = pattern.matcher(originaltext);
    final StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        System.out.println(matcher.group());
        final int index = Integer.parseInt(matcher.group());
        matcher.appendReplacement(sb, values[index]);
    }
    matcher.appendTail(sb);
    System.out.println(sb);
}

输出:

4
2
7
This is DBValue4, This is DBValue2, This is DBValue7

编辑

OP的评论似乎OP需要替换字符串的形式 {name,index} 其中name是数组的名称和index 是该数组中元素的索引。

Further to the OP's comment is seems that the OP needs to replace Strings of the form {name, index} where "name" is the name of an array and "index" is the index of an element in that array.

这很容易通过 Map 将数组ping到它们的名称使用 Map< String,String []> 然后使用模式首先捕获 name 然后索引

This is easily achieved by Mapping the arrays to their names using a Map<String, String[]> and then using a Pattern that captures first the name then the index.

public static void main(String[] args) throws Exception {
    final String[] companies = {"Company1", "Company2", "Company3"};
    final String[] names = {"Alice", "Bob", "Eve"};
    final String originaltext = "This is {company, 0}, This is {name, 1}, This is {name, 2}";
    final Map<String, String[]> values = new HashMap<>();
    values.put("company", companies);
    values.put("name", names);
    final Pattern pattern = Pattern.compile("\\{([^,]++),\\s*+(\\d++)}");
    final Matcher matcher = pattern.matcher(originaltext);
    final StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        final int index = Integer.parseInt(matcher.group(2));
        matcher.appendReplacement(sb, values.get(matcher.group(1))[index]);
    }
    matcher.appendTail(sb);
    System.out.println(sb);
}

输出:

company
0
name
1
name
2
This is Company1, This is Bob, This is Eve

这篇关于在正则表达式中使用反向引用来动态替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:06