Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java字符串替换为数组中具有新模式的正则表达式_Java_Arrays_Regex_String_Replace - Fatal编程技术网

Java字符串替换为数组中具有新模式的正则表达式

Java字符串替换为数组中具有新模式的正则表达式,java,arrays,regex,string,replace,Java,Arrays,Regex,String,Replace,我有一个我无法解决的问题 我有一个段落,其中包含一些关键字,需要用存储在数组中的新值替换 示例: 段落:“我最喜欢的水果是[0],但我也喜欢[1]和[3]。” 数组:水果=[“香蕉”、“橘子”、“苹果”、“葡萄”] 我的期望是:我最喜欢的水果是香蕉,但我也喜欢橘子和葡萄 你能帮我找到解决办法吗 我尝试将我的句子转换为字符串数组,如下所示: [“我最喜欢的水果是”、“[0]”、“,但我也喜欢”、“[1]”、“、和”、“[3]” 之后,我将[0]替换为0,我得到了以下结果: [“我最喜欢的水果是”,

我有一个我无法解决的问题

我有一个段落,其中包含一些关键字,需要用存储在数组中的新值替换

示例:

段落:“我最喜欢的水果是[0],但我也喜欢[1]和[3]。”

数组:水果=[“香蕉”、“橘子”、“苹果”、“葡萄”]

我的期望是:
我最喜欢的水果是香蕉,但我也喜欢橘子和葡萄

你能帮我找到解决办法吗

我尝试将我的句子转换为字符串数组,如下所示:

[“我最喜欢的水果是”、“[0]”、“,但我也喜欢”、“[1]”、“、和”、“[3]”

之后,我将
[0]
替换为
0
,我得到了以下结果:

[“我最喜欢的水果是”,“0”,“但我也喜欢”,“1”,“和”,“3”]

我倾向于将上述数组中的
0
1
3
替换为
水果[0]
水果[1]
水果[3]
的值,然后将该数组转换为完整的字符串


但我认为这不是最好的解决方案,因为如果我得到这样一个输入句子:
“2[2]”
,那么我将收到的输出是AppleApple,而期望值是2Apple

,如果您能够更改
段落的格式,那么您可以从这个片段开始

String paragraph = "My most favorite fruit is %s, but I also like %s and %s";
String[] fruits = {"Banana", "Orange", "Apple", "Grape"};
System.out.printf(paragraph, fruits[0], fruits[1], fruits[2]);                 
输出

My most favorite fruit is Banana, but I also like Orange and Apple
My most favorite fruit is Banana, but I also like Orange and Grape
编辑不必维护水果位置参数的另一个解决方案可以是

String paragraph = "My most favorite fruit is {0}, but I also like {1} and {3}";
Object[] fruits = {"Banana", "Orange", "Apple", "Grape"};
MessageFormat mf = new MessageFormat(paragraph);
System.out.println(mf.format(fruits));
输出

My most favorite fruit is Banana, but I also like Orange and Apple
My most favorite fruit is Banana, but I also like Orange and Grape

您可以使用以下代码(请参阅):

ArrayList\u arr=new ArrayList();//只是初始化数组
添加水果(“香蕉”);
添加水果(橙色);
水果添加(“苹果”);
添加水果(葡萄);
字符串[]fruits=fruits_arr.toArray(新字符串[0]);
String s=“我最喜欢的水果是[0],但我也喜欢[1]和[3]”;
StringBuffer结果=新的StringBuffer();
匹配器m=Pattern.compile(“\\[(\\d+\\\]”)。匹配器;//初始化匹配器
而(m.find()){//迭代匹配
int num=Integer.parseInt(m.group(1));//如果存在匹配项,则组1具有数字
字符串替换=”;
if(num
使用
\[(\d+)
将任何子字符串与
[
+
数字
+
]
匹配,并将数字序列捕获到组1中

使用String.replace

String sentence = "My most favorite fruit is [0], but I also like [1] and [3]";
String[] replacements = {"Banana", "Orange", "Apple", "Grape"};
for(int i = 0; i < replacements.length; i++)
    sentence = sentence.replace("[" + i + "]", replacements[i]);
String-sense=“我最喜欢的水果是[0],但我也喜欢[1]和[3]”;
字符串[]替换={“香蕉”、“橘子”、“苹果”、“葡萄”};
for(int i=0;i
您可以使用以下功能:

String str = "My most favorite fruit is [0], but I also like [1] and [3]";
String[] fruits = { "Banana", "Orange", "Apple", "Grape" };

for (int i = 0; i < fruits.length; i++)
{
    str = str.replaceAll("\\[" + i + "\\]", fruits[i]);
}
String str=“我最喜欢的水果是[0],但我也喜欢[1]和[3]”;
字符串[]水果={“香蕉”、“橘子”、“苹果”、“葡萄”};
for(int i=0;i
Java有一个内置的语法。一般格式为:

%[argument_index$][flags][width][.precision]conversion
因此,您可以使用例如
%1$s
表示第一个格式参数,
%2s
表示第二个格式参数等。请注意,索引是以一为基础的,而不是以零为基础的

e、 g


如果你的问题“无法解决”,你为什么要发布它?我想在源段落中用精确的索引替换,就像[0]应该替换为水果[0],等等…@mrzenky这是否意味着占位符可以是随机顺序。例如,
约翰·多伊喜欢[2]和[1],但不喜欢[3]
的结果应该是
约翰·多伊喜欢苹果和橘子,但不喜欢葡萄
?@zenky您可以使用
%1$s
。看,就像你在用PHP做这个?我可以使用java吗?我已经将代码重新写入java。是的,它类似于PHP、JS和其他语言,我们在Replace函数的回调函数中执行字符串操作。