Java 在正则表达式中使用backreference动态替换文本
我想像使用整数一样使用$1值。Java 在正则表达式中使用backreference动态替换文本,java,regex,backreference,replaceall,Java,Regex,Backreference,Replaceall,我想像使用整数一样使用$1值。 其思想是用等效数组值替换originaltext中的所有数字,并创建一个新文本。 下面的预期结果应该是“这是DBValue4,这是DBValue2,这是DBValue7” 还有,有没有办法保存这些反向引用以供进一步使用 String[] values = {"DBValue0","DBValue1","DBValue2","DBValue3","DBValue4","DBValue5","DBValue6","DBValue7","DBValue8","DBVal
其思想是用等效数组值替换originaltext中的所有数字,并创建一个新文本。
下面的预期结果应该是“这是DBValue4,这是DBValue2,这是DBValue7”
还有,有没有办法保存这些反向引用以供进一步使用
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")]);
您可以像这样使用
模式
和匹配器
:
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);
}
编辑
除了OP的注释之外,OP似乎需要替换{name,index}
格式的字符串
s,其中“name”是数组的名称,“index”是该数组中元素的索引
这可以通过Map
使用Map
将数组ping到它们的名称,然后使用模式来实现,该模式首先捕获名称
,然后捕获索引
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);
}
使用模式
和匹配器
你可以看看这篇文章:很抱歉,为了简单起见,这个例子不太好。您所做的工作是有效的,但我想使用更复杂的内容,如“String originaltext=“{company,3}为{name,2}从{new,11}”;“number{number,10}的所有者发送此消息”和“text=originaltext.replaceAll(\\{.+,(\\d\\d?\\})”,值[Integer.parseInt($1”);”。有什么建议吗?我不明白-所以你有几个数组,你想在字符串上选择它们吗?没有,我只是想用values[an_int]数组属性替换任何文本中的{something,an_int}。如果你做了一些更改,我的编辑应该可以帮你完成。
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